Thursday, October 25, 2007

Small Tips

# print first 10 observations only
proc print data=aa (obs=10);

# apply more than 2 restrictions
proc print data=aa (keep=sex grade where=(sex='m'));

# substr
substr(var, position, length)

# get rid of duplications
proc sort data=aa nodupkey;
by id;

# merge summary statistics
data combined;
if _n_=1 then set summary;
set detail;
run;

# coalesce & coalescec : first non-missing
x = coalesce( ., 42, 52)
x = coalescec ('', 'Goodbye', 'Hello')

# get rid of characters after '.' or '('
loc=indexc(crfpage,'.','(');
if loc>0 then crfpage=substr(crfpage,1,loc-1);


# combine strings
data comb_txt;
input id $4. +1 categry $7.;
aa = compress(categry," ,");
n=length(aa);
cat=substr(aa,1,1);
do i=2 to n;
cat=trim(left(cat))||", "||substr(aa,i,1);
end;
cards;
0149 2, 5
0148 7,
0150 8, 5
0151 3, 4, 6
0152

No comments: