Thursday, March 10, 2011

Regular expressions

1. Search and extract
a. Use Prxmatch before Prxposn


2. Remove whitespace


readhtml
xls2csv to speed up the reading


Thursday, May 8, 2008

Create a variable having value of median in categories

To find the median values of the categories (e.g. to assess test of trend)


%macro hint(var1,var2);

proc means data=fret median;
class &var1;
var &var2;
OUTPUT OUT=&var2 MEDIAN= ;
run;

data null;
set &var2;
call symputx("&var2.m"||put(_n_,1.),&var2,'g');
run;
%put &var2.m;
%put _user_;
%mend;
**depending on the number of categories (n), you would have n+1 macro variables.
**********run like this *************;
%hint(_alcoc,alc) /***_alcoc has 4 categories **/

/*** this would create macro variables as &alcm2 - &alcm(n+1)***/

data new;
set fret;

if _alcoc =. then alcm = .;
else if _alcoc =1 then alcm=&alcm2; /**note this is &alcm2 ***/
else if _alcoc =2 then alcm=&alcm3;
else if _alcoc= 3 then alcm=&alcm4;
else if _alcoc = 4 then alcm=&alcm5;

Thursday, March 27, 2008

Coding interactions/effect modification to get Confidence Intervals

/************ THIS IS 4 X 3 interaction *************/


VARA0VARB0=0; VARA0VARB1=0;VARA0VARB2=0; VARA0VARB3=0;
VARA1VARB0=0; VARA1VARB1=0;VARA1VARB2=0; VARA1VARB3=0;
VARA2VARB0=0; VARA2VARB1=0;VARA2VARB2=0; VARA2VARB3=0;
VARA3VARB0=0; VARA3VARB1=0;VARA3VARB2=0; VARA3VARB3=0;

IF VARA=0 THEN DO;
IF VARB=0 THEN VARA0VARB0=0;
IF VARB=1 THEN VARA0VARB1=1;
IF VARB=2 THEN VARA0VARB2=2;
IF VARB=3 THEN VARA0VARB3=3;
END;

IF VARA=1 THEN DO;
IF VARB=0 THEN VARA1VARB0=0;
IF VARB=1 THEN VARA1VARB1=1;
IF VARB=2 THEN VARA1VARB2=2;
IF VARB=3 THEN VARA1VARB3=3;
END;

IF VARA=2 THEN DO;
IF VARB=0 THEN VARA2VARB0=0;
IF VARB=1 THEN VARA2VARB1=1;
IF VARB=2 THEN VARA2VARB2=2;
IF VARB=3 THEN VARA2VARB3=3;
END;

IF VARA=3 THEN DO;
IF VARB=0 THEN VARA3VARB0=0;
IF VARB=1 THEN VARA3VARB1=1;
IF VARB=2 THEN VARA3VARB2=2;
IF VARB=3 THEN VARA3VARB3=3;
END;

%LET VARB_VARAINT=
VARA0VARB1 VARA0VARB2 VARA0VARB3
VARA1VARB0 VARA1VARB1 VARA1VARB2 VARA1VARB3
VARA2VARB0 VARA2VARB1 VARA2VARB2 VARA2VARB3
VARA3VARB0 VARA3VARB1 VARA3VARB2 VARA3VARB3;

/* To find the point estimates and confidence intervals of each level of VARA with each level of VARB just enter the &VARB_VARAINT
in the models */

/* you can tailor the code with find and replace in SAS/Xemacs/Word/Textpad. */


/************ THIS IS 4 X 4 interaction *************/


VARA0VARB0=0; VARA0VARB1=0; VARA0VARB2=0; VARA0VARB3=0; /* 1ST QUINTILE*/
VARA1VARB0=0; VARA1VARB1=0; VARA1VARB2=0; VARA1VARB3=0; /* 2ND QUINTILE*/
VARA2VARB0=0; VARA2VARB1=0; VARA2VARB2=0; VARA2VARB3=0; /* 3RD QUINTILE */
VARA3VARB0=0; VARA3VARB1=0; VARA3VARB2=0; VARA3VARB3=0; /* 4TH QUINTILE */

IF VARA=0 then do;
if VARB=0 then VARA0VARB0=1;
if VARB=1 then VARA0VARB1=1;
if VARB=2 then VARA0VARB2=1;
if VARB=3 then VARA0VARB3=1;
END;

IF VARA=1 then do;
if VARB=0 then VARA1VARB0=1;
if VARB=1 then VARA1VARB1=1;
if VARB=2 then VARA1VARB2=1;
if VARB=3 then VARA1VARB3=1;
END;

IF VARA=2 then do;
if VARB=0 then VARA2VARB0=1;
if VARB=1 then VARA2VARB1=1;
if VARB=2 then VARA2VARB2=1;
if VARB=3 then VARA2VARB3=1;
END;


IF VARA=3 then do;
if VARB=0 then VARA3VARB0=1;
if VARB=1 then VARA3VARB1=1;
if VARB=2 then VARA3VARB2=1;
if VARB=3 then VARA3VARB3=1;
END;


%LET VARA_VARBINT=
VARA0VARB1 VARA0VARB2 VARA0VARB3
VARA1VARB0 VARA1VARB1 VARA1VARB2 VARA1VARB3
VARA2VARB0 VARA2VARB1 VARA2VARB2 VARA2VARB3
VARA3VARB0 VARA3VARB1 VARA3VARB2 VARA3VARB3 ;

Friday, February 1, 2008

Use a sample to hasten preliminary analysis

I have used following ways;

**************************
proc surveyselect data=onenn method=srs n=10000 out=onen;
run;

**************************
data onen;
merge
fa7684 fa8694 fa9600 nur92 nur94 n94_dt
temp(in=mstr) nur96 nur98 act8600
nur82 nur88 n84_dt n86_dt n90_dt
fileb n767880 meddata
temp db7602
fatalmi mi stroke anginew
fatalstk deadff2004 pact spact end=_end_;

by id;
exrec=1;
if first.id and mstr then exrec=0; /*** mas = master file ie. n80_cf ***/
if famdb82=1 then famdb88=1;
else famdb88=0;
random=RANUNI(-1); /* GENERATE A RANDOM VECTOR */
%let k=5000;
run;

PROC SORT DATA=onen;
BY random; /* SORT OBSERVATIONS BY THE RANDOM VECTOR */
run;

DATA onensample;
SET onen(drop=random);
IF _N_ le &k; /* SELECT THE FIRST K OBSERVATIONS */
/*both magne

Wednesday, June 27, 2007

Concatenating sas macro variables

I always forget how to do it.

%let nagasuchi=cases;
data new;
set library.old&nagasuchi;

This would be read as
data new;
set library.oldcases;


If the macro variable is a prefix

data new;
set library.&nagasuchi.old;
/* note the period*/

This would be read as
data new;
set library.casesold;

If the character following a macro variable is a period, then you need to use two periods.
set in&nagasuchi..select;

After resolution, SAS would read this as SET incases.select;



More on this here.