SAS

Frequently used scripts

 

PROC MEANS STACK DATA OPTION
proc means data=kaz5 STACKODSOUTPUT ;
class treat;
var Z_GRADE_AVERAGE STEM ;
ods output summary=niko1;
run;

http://support.sas.com/documentation/cdl/en/proc/65145/HTML/default/viewer.htm#p17h6q7ygvkl1sn13qzf947dundi.htm

 

Take out strings at the beginning of character variables (and leave numeric)

digit=anydigit(studentID);
val=substr(studentID,digit);

 

Take the first and last observations (horizontally)

data one;
input Jan Feb March April;
cards;
11 . 32 .
. 33 22 12
12 . . .
;

data new(drop=i);
set one;
flag='0';
array vars(*) _numeric_;
retain flag;
do i = 1 to dim(vars);
if vars(i) ne . and flag ne '1' then do;
first=vars(i);
flag='1';
first_month=vname(vars(i));
end;
else if vars(i) ne . then do;
second=vars(i);
last_month=vname(vars(i));
end;
end;
if second=. then do;
second = first;
last_month=first_month;
end;

run;

proc print;
run;

 

Cronbach Alpha using PROC CORR

Cronbach Coefficient Alpha

 

Essential SAS techniques

Identify duplicate rows using PROC SQL.  This returns the number of duplicative rows per ID (e.g., email address)

proc sql;
create table all2 as
select *,
count(email) as duplic_count
from all
group by email;
run;

 

Export SAS datasets into Excel

proc export data=sashelp.class

outfile='C:\sastest\class.xlsx'

dbms=xlsx replace;

sheet='Class';

run;

 

PROC SQL and how to normalize/adjust weights (such that the sum of weights = the number of cases in the sample).  This runs in any PCs as it uses sashelp.class (the dataset that comes with SAS installation).

proc sql;
create table newdata1 as
select *,
weight * (count(weight)/Sum(weight)) as Adjusted_weight1
from sashelp.class;
proc sql;
create table newdata2 as
select *,
weight * (count(weight)/Sum(weight)) as Adjusted_weight2
from newdata1
group by sex;

 

Functions

These concatenate variables but keep a space between variabels.

subgroup=compbl(school_level||categorydesc||outcomesubstance2);

subgroup=catx(' ',school_level,categorydesc,outcomesubstance2);

 

Replace a character with another character (or in this case, I'm replacing "_" with nothing (""), which means I'm removing "_".

Affilication=TRANWRD(Affilication,"_","") ;

Concatenate variables SAS LINK .  First variable specified (x1 in this example) will be used to separate variables (e.g., if x1=" ", a blank will be used to separate values from x2 and x3).

x=catx(x1 x2 x3);

Quickly create a specific date variable:

data temp;
x='01APR2016'D;
FORMAT x date9. ;run;
run;

Create a date variable using MDY

FORMAT Query_date date9. ;
Query_date=MDY(1,23,2016);

Create a text file with variables

data _null_;set kaz2t2;
blank=' ';
file "c:\temp\example.txt"; /*you can change this*/
put
(syntax) (500.0);
run;

 

Remove labels

My example:

proc datasets library=work nolist;
modify all;
attrib _all_ label=";
quit;

Easy Way to Remove All Variable Labels in SAS

 

Make all values in text variables expressed in capital letters.

http://support.sas.com/kb/39/525.html