BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
robertrao
Quartz | Level 8

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);

1 ACCEPTED SOLUTION

Accepted Solutions
Linlin
Lapis Lazuli | Level 10

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")

View solution in original post

9 REPLIES 9
Linlin
Lapis Lazuli | Level 10

is your variable "Proc_code" a numeric or character variable?

robertrao
Quartz | Level 8

Thanks for the reply LINLIN. Its charecter variable

Linlin
Lapis Lazuli | Level 10

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");

robertrao
Quartz | Level 8

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.

Linlin
Lapis Lazuli | Level 10

then try:

%cohorts(want=headache,code="32" "34" "36" "38");

robertrao
Quartz | Level 8

Linlin,

It works perfectly fine now. Whats the trick there?

Why shouldnt we seperate them by a comma????

PLease explain

Thanks

ballardw
Super User

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.

Linlin
Lapis Lazuli | Level 10

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").

Linlin
Lapis Lazuli | Level 10

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")

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is ANOVA?

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.

Discussion stats
  • 9 replies
  • 1711 views
  • 6 likes
  • 3 in conversation