DATA Step, Macro, Functions and more

Getting a list of variables to resolve correctly in a macro

Accepted Solution Solved
Reply
Contributor
Posts: 25
Accepted Solution

Getting a list of variables to resolve correctly in a macro

I have 100,000 patients, each of which receives one of 116 diagnoses. I'm exploding the 116 diagnoses into 116 binary variables. Some of these diagnoses are rare, so I want to roll them up into an diagnosis that is much more prevalent, and pare down the list of 116 to around 30 common diagnoses. I'm using the code below to try to do that, but the %if condition always returns a FALSE. That should not be the case for patients with rare diagnoses. Do you know what is wrong with my code?

 

Thanks!

 

Andrew

 

options minoperator;

%macro aggdx(dxlist, rolled_up_dx);

   %if diagnosis in &dxlist %then &rolled_up_dx = 1;

%mend;

 

data hosp1; set hosp2;

   /* Example of what I want to do:

        if acidbase = 1 or comametu = 1 or diabetic = 1 then metamisc = 1;

*/

   %aggdx(ACIDBASE COMAMETU DIABETIC, METAMISC)

/* So I'll have about 30 calls to %aggdx, one for each of the common diagnoses */ ;

run;

Andrew A. Kramer, PhD
President
Prescient Healthcare Consulting

Accepted Solutions
Solution
‎06-03-2016 02:29 PM
Trusted Advisor
Posts: 1,115

Re: Getting a list of variables to resolve correctly in a macro

Hello @DocMartin,

 

You are confusing data step and macro logic: %if diagnosis in &dxlist ... checks whether the string diagnosis (not the value of a data step variable of this name!) is contained in the value of the first macro parameter (ACIDBASE ... in your example).

 

So, if diagnosis is a character variable in dataset HOSP2, containing diagnoses like "ACIDBASE" etc., the following macro definition should work:

 

%macro aggdx(dxlist, rolled_up_dx);
  if diagnosis in ("%sysfunc(tranwrd(&dxlist,%str( )," "))") then &rolled_up_dx = 1;
%mend;

Now, IF/THEN as well as IN are data step code generated by the macro. In particular, the option MINOPERATOR is not necessary for this to work.

 

If you want to use your binary variables instead of the character variable DIAGNOSIS, the macro code will be different, of course.

 

View solution in original post


All Replies
Solution
‎06-03-2016 02:29 PM
Trusted Advisor
Posts: 1,115

Re: Getting a list of variables to resolve correctly in a macro

Hello @DocMartin,

 

You are confusing data step and macro logic: %if diagnosis in &dxlist ... checks whether the string diagnosis (not the value of a data step variable of this name!) is contained in the value of the first macro parameter (ACIDBASE ... in your example).

 

So, if diagnosis is a character variable in dataset HOSP2, containing diagnoses like "ACIDBASE" etc., the following macro definition should work:

 

%macro aggdx(dxlist, rolled_up_dx);
  if diagnosis in ("%sysfunc(tranwrd(&dxlist,%str( )," "))") then &rolled_up_dx = 1;
%mend;

Now, IF/THEN as well as IN are data step code generated by the macro. In particular, the option MINOPERATOR is not necessary for this to work.

 

If you want to use your binary variables instead of the character variable DIAGNOSIS, the macro code will be different, of course.

 

Super User
Posts: 10,516

Re: Getting a list of variables to resolve correctly in a macro

IN doesn't work quite that way. It want's explicit values. If you were doing a comparison with another variable created that held the name of the diagnoses then the names would have to be quoted.

 

If all of your codes are binary you could test to see if the MAXIMUM of any of the values is 1 for a similar comparison

 

so

If Max( ACIDBASE, COMAMETU, DIABETIC) = 1 then metamisc = 1;

would be equivalent. Which requires a comma delimited list. So this may work for you:

%macro aggdx(dxlist, rolled_up_dx);
   %let Dxlist= %sysfunc(translate(&dxlist,%str(,),%str( )));

  if max(&dxlist)=1 then &rolled_up_dx = 1;

%mend;

 

 

 

You were also confusing MACRO comparisons %if /%then with the desired Datastep code if then.

Respected Advisor
Posts: 4,651

Re: Getting a list of variables to resolve correctly in a macro

"Do you know what is wrong with my code?"

Yes, you are using macros.

 

You simply need to build a dataset with Diagnosis - Common Diagnosis equivalences and use it to recode your observations. The last part can be done with a datastep MERGE operation, a format recode, or a SQL join. 

 

Keep macros for cases where everything else fails. 

 

PG
☑ This topic is SOLVED.

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

Discussion stats
  • 3 replies
  • 223 views
  • 0 likes
  • 4 in conversation