Sunday, July 26, 2009

remove formats or labels

This tip comes by way of sastips.com

sometimes

The following example of code removes all labels and formats for the dataset EXAMPLE in the directory referenced by the LIBNAME WORK:

    proc datasets lib=work nolist;
modify example;
attrib _all_ label=''; *Remove labels;
format _all_; *Remove formats;
quit;
run;

Tuesday, July 1, 2008

sas dates

Dealing with SAS dates can be tricky, particularly if you're migrating your data between different analysis platforms (SAS in UNIX, R, JMP, etc.

In order to convert a date into the sasdate, you can use one of 2 methods, depending on whether the variable was read in as a character or numeric variable.

method 1: newsasdate=input(olddate, date11.);

method 2:newsasdate=input(put(olddate, Z6.) ddmmyy6.

Monday, March 17, 2008

magic code to transform R data with 'NA' for missing to SAS '.'

array inc dlfib--dlcrp;
do i = 1 to 4;
if inc[i]='NA' then inc[i]=.;
end;
drop i;
run;

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;