Friday 15 March 2013

Creating new variable in SAS -



Creating new variable in SAS -

i have panel dataset in sas, looks this:

data have; input id time income; cards; 1 2008 1000 1 2009 900 1 2010 1100 2 2008 600 2 2009 500 2 2010 400 3 2008 300 3 2009 350 3 2010 250 ; run;

for each individual, want create new column (named income_id) income of individual in time periods , 0 other individuals. want this:

data want; input id time income income_1 income_2 income_3; cards; 1 2008 1000 1000 0 0 1 2009 900 900 0 0 1 2010 1100 1100 0 0 2 2008 600 0 600 0 2 2009 500 0 500 0 2 2010 400 0 400 0 3 2008 300 0 0 300 3 2009 350 0 0 350 3 2010 250 0 0 250 ; run;

thanks

an intuitive way using macros.

there sugi yunchao tian explaining how perform task here.

i adapted code here you. tested , seems work alright.

proc sort data=have out=unique nodupkey; id; run; /* assign largest value of id macro variable nmax */ info _null_; set unique end=last; if lastly phone call symput('nmax', put(id, 3.)); run; /* create macro variables , assign value 0*/ info _null_; i=1 &nmax; phone call symput('m'||left(put(i,3.)), '0' ); end; run; /* assign value of id corresponding macro variable */ info _null_; set have; phone call symput('m'||left(put(id,3.)), put(id,3.)); run; /* macro create code set col income or 0 */ %macro getid; %do = 1 %to &nmax; %if &&m&i = 0 %then %goto out; if id = &&m&i income_&i = income; else income_&i = 0; %out: %end; %mend getid; /* execute macro */ info want; set have; %getid run; proc print data=want; run;

sas

No comments:

Post a Comment