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.

Friday, June 15, 2007

LaTeX output in sas

/*Use one of the following ods statements */
/* Legacy LaTeX for ODS */
ods tagsets.latex file="legacy.tex";

/* Legacy LaTeX with color for ODS */
ods tagsets.colorlatex file="color.tex" stylesheet="sas.sty"(url="sas");

/* Simplified LaTeX output that uses plain LaTeX tables */
ods tagsets.simplelatex file="simple.tex" stylesheet="sas.sty"(url="sas");

/* Same as above, but only prints out tables (no titles, notes, etc.) */
/* Also, prints each table to a separate file */
ods tagsets.tablesonlylatex file="C:\Documents and Settings\mk\My Documents\tablesonly.tex" (notop nobot) newfile=table;

proc reg data=sashelp.class;
model Weight = Height Age;
run;quit;

/*Use one of the following ods statements corresponding to open statements*/

ods tagsets.latex close;
ods tagsets.colorlatex close;
ods tagsets.tablesonlylatex close;
ods tagsets.simplelatex close;

/*from SAS*/

Thursday, May 3, 2007

Unix Banner for interesting titles

ASCII Banner 1 , 2


  .'|.           .                                   '||  
.||. .... .||. .... .... .. ... .. ||
|| '' .|| || ||. ' '' .|| || || .' '||
|| .|' || || . '|.. .|' || || || |. ||
.||. '|..'|' '|.' |'..|' '|..'|' .||. ||. '|..'||.


||
.... .. ... ... . ... .. ... ....
'' .|| || || || || || || || '' .||
.|' || || || |'' || || || .|' ||
'|..'|' .||. ||. '||||. .||. .||. ||. '|..'|'
.|....'


  __       _                         _                     _            
/ _| __ _| |_ ___ __ _ _ __ __| | __ _ _ __ __ _(_)_ __ __ _
| |_ / _` | __/ __| / _` | '_ \ / _` | / _` | '_ \ / _` | | '_ \ / _` |
| _| (_| | |_\__ \ | (_| | | | | (_| | | (_| | | | | (_| | | | | | (_| |
|_| \__,_|\__|___/ \__,_|_| |_|\__,_| \__,_|_| |_|\__, |_|_| |_|\__,_|
Have fun. Don´t forget to bookmark this website :)


Thursday, April 19, 2007

Make results more presentable

SAS regression output requires additional steps to make it presentable. In previous posts, I have highlighted how to only get the results for variables of interest. However, these results are in row format

Obs F1 or lci uci p_value

1 exp1 1.202 1.110 1.34 .001
2 exp2 1.340 1.202 1.56 .001
3 exp3 1.560 1.340 1.89 .001
4 exp4 1.890 1.560 1.98 .001



This output need to be further transposed in Excel to get the results in following format.

Obs exp0 exp1 exp2 exp3 exp4

1 1 1.2020 1.3400 1.5600 1.8900
2 1.11,1.34 1.202,1.56 1.34,1.89 1.56,1.98
3 0.0010 0.0010 0.0010 0.0010


The following program eliminates that

proc import datafile="C:\Documents and Settings\mkaushik\Desktop\Output results.xls" out=auto replace;
run;
data inter /* / view=intermediate*/;
set auto;
orc= put(or,6.4);
pvaluec= put(p_value,6.4);
new=compress(lci||','||uci);
output; *output the input record;
if _n_=1 then do;
F1='exp0';
or=1; *set values for your added obs;
lci=.;uci=.;p_value=.;
orc="1";
pvaluec=" ";
new=" ";
output; *output your added obs;
end;
proc sort;
by F1;
run;
data inter;
set inter;
array Value (*) orc new pvaluec;
do id =1 to 3;
_value_ = value [id]; * since first numeric is that date;
F1=F1;
output;
end;
drop or p_value _name_;
run;
proc print data=inter;
run;

proc sort data=inter;
by id;
run;
proc transpose data=inter out=outset(drop=id _name_) ;
by id ;
id F1 ;
var _value_;
run;
proc print data=outset;
run;