/* Joel Elvery. 10-25-05 goodlist: takes a string and creates a set of consecutively numbered global variables. Useful to get sas to do forloops etc. pre=prefix to macro variable names (the suffix will be consecutive numbers (1,2,...) string=a list of whatever you want (I have not tested that assertion too much) type=tells if list is numeric or character variables. Creates: set of global variables pre1-pre&pre_num and pre_num, which is the number of elements in the list. sample case: Make a bunch of vars for different ages from NLSY %goodlist(y,79 80 81 82 83 84,n); %goodlist(a,25 28 30,n); %goodlist(v,hrp occup indus,c); %do i=1 %to &y_num; %do j=1 %to &a_num; %do k=1 %to &v_num; if ageint&&y&i=&&a&j then &&v&k.&&a&j=&&v&k.&&y&i; %end; %end; %end; Will end up with: hrp25 occup25 indus25 hrp28 occup28 indus28 hrp30 occup30 indus30 If you have a long set of commands you are loop over and having to write &&y&i etc. annoys you, you can use it like this: %do i=1 %to &y_num; %do j=1 %to &a_num; %do k=1 %to &v_num; %local a y v; %let a=&&a&j; %let y=&&y&i; %let v=&&v&k; if ageint&y=&a then &v.&a=&v.&y; %end; %end; %end; Cool, eh? Works a lot like a foreach loop in stata. */ %macro goodlist(pre,string,type); %local count j; %let count=1; %let j=%qscan(&string,&count,%str( )); %do %while(&j ne); %global &pre.&count; %if "&type"="n" %then %do; %let &pre.&count=%eval(&j); %end; %if "&type"="c" %then %do; %let &pre.&count=%cmpres(&j); %end; %let count=%eval(&count+1); %let j=%qscan(&string,&count,%str( )); %end; %global &pre._num; %let &pre._num=%eval(&count - 1); %mend; * goodlist; /* program you can run to see how it works */ /* look towards the bottom of the log file for the results */ /* note: can not use %do outside of a macro, so program is in a simple macro */ /* %macro runit; %goodlist(n,Blair Bush,c); %goodlist(d,President American,c); data _null_; %do i=1 %to &n_num; %do j=1 %to &d_num; if "&&n&i"="Blair" then put "&&n&i is not &&d&j."; else put "&&n&i is &&d&j."; %end; %end; run; %mend; %runit; */ /************************************************************************/ /************************************************************************ Joel Elvery. 09-04-06 newdummy: Efficiently makes dummy variables so you do not have to resort original data. The dummy name includes the categorical value, so it is easy to tell what a dummy represents and to set one to zero to use as an ommited group. Syntax: %newdummy(indata,var,abrv) indata: Data set adding dummy variables to. var: Name of categorical variable you want to turn into a set of dummies. abrv: The name stem. This will determine what dummy name starts with and what the global variable that allows you to refer to list of variables ends with. Produces: Set of dummy variables name abrv_X, where X is an actual categorical value (e.g. st_ca or st_06 for a California dummy variable). Also creates a list of the variables, l_abrv, so can refer to them in a regression as &l_abrv rather typing a list. ************************************************************************/ /************************************************************************/ %macro newdummy(indata,var,abrv); proc freq data=&indata(keep=&var) noprint; tables &var / out=ndtemp; data _null_; set ndtemp nobs=k; call symput("tmp"||trim(left(put(_n_,20.))),trim(left(put(&var,20.)))); call symput("tmpmax",k); data &indata; set &indata; %do i=1 %to %eval(&tmpmax); &abrv._&&tmp&i=(left(&var)=left("&&tmp&i")); %end; %global l_&abrv; %let l_&abrv= ; %do i=1 %to %eval(&tmpmax); %let l_&abrv=&&l_&abrv &abrv._&&tmp&i; %end; %mend; /* newdummy */