Saturday, July 14, 2007

use "where" statements to print observations for selected critieria

In this example I want to output data for specific ids I've selected in SAS. This turns out to be easier said than done...
The correct syntax for this is as follows.

proc print data=alldat2;
24725 where id=1 or id=2 or id=3;
24726 run;

NOTE: There were 1 observations read from the data set WORK.ALLDAT2.
WHERE id in (1, 2, 3);
NOTE: The PROCEDURE PRINT printed pages 4-14.
NOTE: PROCEDURE PRINT used (Total process time):
real time 22.44 seconds
cpu time 2.51 seconds


voila.

Sunday, June 24, 2007

use "retain statement" to make a count variable

**This code comes from UCLA statistical computing seminar
(link here to code)

proc sort data=kids out=sort_kids;
by famid;
run;
data retain1;
set sort_kids;
retain sumwt count;

/*carry over the value from previous obs to next obs*/
by famid;
if first.famid then do;
/*at 1st obs of each family set sumwt and count = 0*/
sumwt=0;
count=0;
end;
sumwt = sumwt + wt;
count = count + 1;
meanwt = sumwt/count;
run;
proc print data=retain1;
var famid kidname wt sumwt count meanwt;
run;

Wednesday, May 30, 2007

code for transforming data from multivariate format to the univariate format

data data;
infile 'c:\myfiles\testfile.dat';
input id y0 y1 y2 y3;
y=y0 trt=0; output;
y=y1 trt=1; output;
y=y2 trt=2; output;
drop y0-y2;
run;

Tuesday, February 13, 2007

quartiles (or x-tiles) for the lazy

data composite;
set data.composite;
proc rank groups=4 out=ranked;
var srcalc srprot sriron srmagnes srphos srzinc srvitc srvitd packyrs bmi;
ranks rank_srcalc rank_srprot rank_sriron rank_srmagnes rank_srphos rank_srzinc rank_srvitc rank_srvitd rank_packyrs rank_bmi;
run;

Sunday, February 11, 2007

create a categorical variable in one fell swoop

categorize a variable with "lazy elegance"

if age ne . then agecat= int(iage/5-1);

make a good looking histogram

More often than would be preferable, it becomes necessary to present data graphically. For a fancy ODS plot of a histogram, try the following:

ods pdf file ="%sysfunc (reverse(%sysfunc(substr(%sysfunc(reverse(%sysfunc(reverse(%scan(%sysfunc(reverse(%sysfunc(getoption(sysin)))),1,/))))),5)))).pdf"; goptions reset=global device=gif ftitle=swissb ftext=swissb htitle=2 htext=2; proc univariate data = alldat2 noprint; title "Histogram for pmh variables"; histogram pmh_dur3 pmh_dur4 pmh_dur5 pmh_dur8 drh_use lst_byirt/cfill=ligr normal cframe=liy barwidth=8 cv=black; inset mean std max min;

ods pdf close;

transpose data

Transpose your data from the so-called "long format" to the ever popular and
occassionally useful "wide format"



proc transpose data=long2 out=widef prefix=faminc;
by famid;
id year;
var faminc;
run;

proc transpose data=long2 out=wides prefix=spend;
by famid;
id year;
var spend;
run;

data wide2;
merge widef(drop=_name_) wides(drop=_name_);
by famid;
run;