This widget could not be displayed.
This widget could not be displayed.
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.

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
This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.

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
This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.

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

This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.

Thanks for the reply LINLIN. Its charecter variable

This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.

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

This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.

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.

This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.

then try:

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

This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.

Linlin,

It works perfectly fine now. Whats the trick there?

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

PLease explain

Thanks

This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.

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.

This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.

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

This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.

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-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and save with the early bird rate—just $795!

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
  • 3060 views
  • 6 likes
  • 3 in conversation
This widget could not be displayed.
This widget could not be displayed.