Programming the statistical procedures from SAS

MACRO CODE

Accepted Solution Solved
Reply
Super Contributor
Posts: 1,040
Accepted Solution

MACRO CODE

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


Accepted Solutions
Solution
‎09-28-2012 04:14 PM
Super Contributor
Posts: 1,636

Re: MACRO CODE

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


All Replies
Super Contributor
Posts: 1,636

Re: MACRO CODE

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

Super Contributor
Posts: 1,040

Re: MACRO CODE

Thanks for the reply LINLIN. Its charecter variable

Super Contributor
Posts: 1,636

Re: MACRO CODE

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

Super Contributor
Posts: 1,040

Re: MACRO CODE

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.

Super Contributor
Posts: 1,636

Re: MACRO CODE

then try:

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

Super Contributor
Posts: 1,040

Re: MACRO CODE

Linlin,

It works perfectly fine now. Whats the trick there?

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

PLease explain

Thanks

Super User
Posts: 10,871

Re: MACRO CODE

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.

Super Contributor
Posts: 1,636

Re: MACRO CODE

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

Solution
‎09-28-2012 04:14 PM
Super Contributor
Posts: 1,636

Re: MACRO CODE

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

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

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