Hi Team, Could you please check if my macro needs to be made tidy or is this logic OK?
I have to subset observations into seperate cohorts(datasets) based on the codes
Sometimes using only 1 or 2 codes and sometimes using as many as 12 codes.
I did a macro because the A to J variables are common for all the "want" datasets and are coming from the same dataset "have"
I dont want to repeat the same steps 10 times
Would you recommend me to go ahead or u have any suggestions??
Thanks
%macro cohorts(want=,code1=,code2=,code3=,code4=,code5=,code6=,code7=,code8=,
code9=,code10=,code11=,code12=);
data &want;
set have(keep=A B C D E F G H I J );
where Proc_code in("&code1","&code2","&code3","&code4","&code5","&code6","&code7","&code8","&code9",
"&code10","&code11","&code12");
A1=put(A,agefmt.);
B1=compress(B);
B2=put(B1,$sexfmt.);
C1=put(C,BMIFMT.);
D1=D/3600;
D2=put(D1,OPTIMEFMT.);
E1=put(E,$DIA.);
F1=STRIP(F);
F2=put(F1,$SMOK.);
if substr(Pat,5) in("A", "B" ,"C","D","E") then pat=substr(Pat,1,4);else pat=Pat;
if Pat eq " " then delete;
run;
%mend;
%cohorts(want=headache,code1=32,code2=34,code3=36,code4=38);
%cohorts(want=fever,code1=57,code2=58);
%cohorts(want=nausea,code1=29);
%cohorts(want=jaundice,code1=630,code2=632,code3=633,code4=634,code5=635,code6=636,
code7=638,code8=639,code9=647,code10=677,code11=678,code12=679);
It should work. You code is similar to the example below.
data have;
set sashelp.class;
proc_code=put(age,2.);
run;
%macro cohorts(want=,code=);
data &want;
set have;
where Proc_code in(&code) ;
%mend;
%cohorts(want=headache,code="11" "12" "13" "38")
%cohorts(want=happy,code="15" "16")
is your variable "Proc_code" a numeric or character variable?
Thanks for the reply LINLIN. Its charecter variable
I made some changes to your code. Please let me know if it works or not.
%macro cohorts(want=,code=)
data &want;
set have(keep=A B C D E F G H I J );
where Proc_code in(&code) and pat ne ' ';
A1=put(A,agefmt.);
B1=compress(B);
B2=put(B1,$sexfmt.);
C1=put(C,BMIFMT.);
D1=D/3600;
D2=put(D1,OPTIMEFMT.);
E1=put(E,$DIA.);
F1=STRIP(F);
F2=put(F1,$SMOK.);
if substr(Pat,5,1) in("A", "B" ,"C","D","E") then pat=substr(Pat,1,4);
run;
%mend;
%cohorts(want=headache,code="32","34","36","38");
Hi LINLIN,
There is an error which says:
46 %cohorts(want=graft,code="15732","15734","15736","15738");
-------
180
ERROR: All positional parameters must precede keyword parameters.
ERROR 180-322: Statement is not valid or it is used out of proper order.
then try:
%cohorts(want=headache,code="32" "34" "36" "38");
Linlin,
It works perfectly fine now. Whats the trick there?
Why shouldnt we seperate them by a comma????
PLease explain
Thanks
The comma is no longer required for use with the IN operator though much sample code will use commas as it was previously require. I'm not sure which exact version made the change.
Comma separates the macro variables. when we use "%cohorts(want=headache,code="11" ,"12" ,"13", "38")".
macro variable code only takes one value (code="11").
It should work. You code is similar to the example below.
data have;
set sashelp.class;
proc_code=put(age,2.);
run;
%macro cohorts(want=,code=);
data &want;
set have;
where Proc_code in(&code) ;
%mend;
%cohorts(want=headache,code="11" "12" "13" "38")
%cohorts(want=happy,code="15" "16")
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.
Find more tutorials on the SAS Users YouTube channel.