| Date: | Fri, 29 Aug 1997 11:59:16 EDT |
| Reply-To: | Lorna Simon <Lorna.Simon@BANYAN.UMMED.EDU> |
| Sender: | "SAS(r) Discussion" <SAS-L@UGA.CC.UGA.EDU> |
| From: | Lorna Simon <Lorna.Simon@BANYAN.UMMED.EDU> |
| Subject: | Using macros to create date variables - solution (long) |
|---|
Dear SAS-lers,
I wanted to thank Karsten Self, Jerome Kramer, Martyn Byng, Tom Abernathy
Randy Childs, Anthony Ayiomamtis, Greg Silva and anyone else I might have
forgotten for their help on fixing my macro. Following is an excerpt from
my original message:
---------------------------------------------------------------------------
I am trying to create 28 variables marking the start and end of 14 quarters,
based on a patient's admission date. The variables for the start of the
quarters are named q1start--q14start, and the variables for the end of the
quarters are named q1end--q14end. In the code below, the macro %countall
creates the variable names for the start and end of the quarters. This macro
seems to be working as I would like it to. In the macro %qassign, I am
trying to get SAS to assign values to the
variables. This macro is not working.
*Create inpatient dataset for clients with admissions in each period;
data inpat1;
set cts.inp;
call symput('scount',left(put(0,2.)));
call symput('ecount',left(put(0,2.)));
call symput('iter',left(put(1,2.)));
run;
proc sort data=inpat1; by admdate; run;
%macro countall;
%do i=1 %to 14;
%let scount=%eval(&scount + 1);
%let ecount=%eval(&ecount + 1);
%let start=q&scount.start;
%let end=q&ecount.end;
%let iter=%eval(&iter + 1);
%put &scount &ecount &start &end &iter;
%end;
%mend countall;
%countall
data inpat2;
set inpat1;
call symput('start',%eval(mdy(7,1,91)));
call symput('end',%eval(mdy(9,30,91)));
call symput('itr',left(put(1,2.)));
%macro qassign;
%do i=1 %to 14;
%if (admdate lt &start and admdate ne .) %then %do;
%let start=%eval(&start + 91);
%let end=%eval(&end + 91);
%let itr=%eval(&itr + 1);
%put &start &end mmddyy10.;
%end;
%end;
%mend qassign;
%qassign
run;
-----------------------------------------------------------------------------
Several people reminded me of the intnx function, which will calculate a
value by advancing a value or variable by a specified number of time
intervals. This of course, worked much better than adding 91 days to each of
the "start" and "end" variables. Someone told me that the macro %qassign was
not working as I thought, and that I was just ending up with one "start" and
one "end" variable and not 14 "start" and "end" variables, as I had thought.
Several people told me that to do what I wanted I needed an array, and not a
macro to create the "start" and "end" variables. The code that does this
follows:
*Create inpatient dataset for clients with admissions in each period;
data inpat1;
set cts.inp;
array start{*} q1start q2start q3start q4start q5start q6start q7start
q8start q9start q10start q11start q12start q13start q14start;
array end{*} q1end q2end q3end q4end q5end q6end q7end q8end q9end q10end
q11end q12end q13end q14end;
do i=2 to 14;
start{i}=intnx('qtr',start{i-1},1);
end{i}=intnx('qtr',end{i-1},1);
end;
run;
---------------------------------------------------------------------------
I did, however, need a macro to do the rest of the work, because I wanted to
generate the code which would assign a value to 14 variables (Originally, I
was trying to assign a value to 14 "start" and 14 "end" variables. After
further thought, I realized that what I wanted to do was assign a number to a
variable called "quarter," based on a patient's admission date. I apologize
for any confusion this might have caused.) The code to do this follows, and
was mostly written by Randy Childs.
%macro qassign(count=);
%do i=1 %to &count;
%str(if (admdate ge q&i.start and admdate ne .) then do;
quarter=&i;
end;)
%end;
%mend qassign;
data inpat2;
set inpat1;
%qassign(count=14)
run;
As I understand it, assigning a value of 14 to the macro variable count when
the macro executes casues the code to be written and executed 14 times for
each of the 14 "start" variables. Based on the admission date, each record is
assigned to a quarter.
This group is a great resource for anyone trying out a new (to them) function
or module in SAS I certainly have learned a lot and have a much better
understanding of macros form trying out people's suggestions. Once again,
thanks to everyone for their help.
Lorna Simon
Lorna J. Simon, M.A., Research Associate
Center for Psychosocial & Forensic Services Research
Department of Psychiatry
University of Massachusetts Medical School
55 Lake Avenue North
Worcester, MA 01655-0329
Phone: 508-856-5498, 508-793-1550
FAX: 508-754-5007
email: Lorna.Simon@banyan.ummed.edu
|