I was wondering if there is any simple code to do the codes below:
if (R9HIBP=1 or R9HIBPE =1) then highbp9=1; else highbp9=0;
if (R9DIAB =1 or R9DIABE =1) then diabetes9=1; else diabetes9=0;
if (R9CANCR =1 or R9CANCRE =1) then cancer9=1; else cancer9=0;
if (R9HEART =1 or R9HEARTE =1) then heartd9=1; else heartd9=0;
if (R9STROK =1 or R9STROKE =1) then stroke9=1; else stroke9=0;
if (R9lung =1 or R9lungE =1) then lung9=1; else lung9=0;
if (R10HIBP=1 or R10HIBPE =1) then highbp10=1; else highbp10=0;
if (R10DIAB =1 or R10DIABE =1) then diabetes10=1; else diabetes10=0;
if (R10CANCR =1 or R10CANCRE =1) then cancer10=1; else cancer10=0;
if (R10HEART =1 or R10HEARTE =1) then heartd10=1; else heartd10=0;
if (R10STROK =1 or R10STROKE =1) then stroke10=1; else stroke10=0;
if (R10lung =1 or R10lungE =1) then lung10=1; else lung10=0;
if (R11HIBP=1 or R11HIBPE =1) then highbp11=1; else highbp11=0;
if (R11DIAB =1 or R11DIABE =1) then diabetes11=1; else diabetes11=0;
if (R11CANCR =1 or R11CANCRE =1) then cancer11=1; else cancer11=0;
if (R11HEART =1 or R11HEARTE =1) then heartd11=1; else heartd11=0;
if (R11STROK =1 or R11STROKE =1) then stroke11=1; else stroke11=0;
if (R11lung =1 or R11lungE =1) then lung11=1; else lung11=0;
@nwang5 wrote:
I was wondering if there is any simple code to do the codes below:
if (R9HIBP=1 or R9HIBPE =1) then highbp9=1; else highbp9=0;
if (R9DIAB =1 or R9DIABE =1) then diabetes9=1; else diabetes9=0;
if (R9CANCR =1 or R9CANCRE =1) then cancer9=1; else cancer9=0;
if (R9HEART =1 or R9HEARTE =1) then heartd9=1; else heartd9=0;
if (R9STROK =1 or R9STROKE =1) then stroke9=1; else stroke9=0;
if (R9lung =1 or R9lungE =1) then lung9=1; else lung9=0;
if (R10HIBP=1 or R10HIBPE =1) then highbp10=1; else highbp10=0;
if (R10DIAB =1 or R10DIABE =1) then diabetes10=1; else diabetes10=0;
if (R10CANCR =1 or R10CANCRE =1) then cancer10=1; else cancer10=0;
if (R10HEART =1 or R10HEARTE =1) then heartd10=1; else heartd10=0;
if (R10STROK =1 or R10STROKE =1) then stroke10=1; else stroke10=0;
if (R10lung =1 or R10lungE =1) then lung10=1; else lung10=0;
if (R11HIBP=1 or R11HIBPE =1) then highbp11=1; else highbp11=0;
if (R11DIAB =1 or R11DIABE =1) then diabetes11=1; else diabetes11=0;
if (R11CANCR =1 or R11CANCRE =1) then cancer11=1; else cancer11=0;
if (R11HEART =1 or R11HEARTE =1) then heartd11=1; else heartd11=0;
if (R11STROK =1 or R11STROKE =1) then stroke11=1; else stroke11=0;
if (R11lung =1 or R11lungE =1) then lung11=1; else lung11=0;
Yes, you can replace all these lines of data step code by a single macro call, for example:
data want; set have; %recode(9,11,HIghBP DIABetes CANCeR HEARTd STROKe LUNG) run;
after defining the macro, e.g., like this:
%macro recode(first, last, dis);
%local i j var rvar;
%do i=&first %to &last;
%do j=1 %to %sysfunc(countw(&dis));
%let var=%scan(&dis,&j);
%let rvar=R&i%upcase(%sysfunc(compress(&var,,ku)));
%unquote(%lowcase(&var)&i = (&rvar=1 | &rvar.E=1);)
%end;
%put;
%end;
%mend recode;
So you supply the first and last wave number (?) and the "disease list" (with uppercase letters indicating how to form the abbreviated variable names) in the three macro parameters and the macro creates the assignment statements (which are equivalent to your IF-THEN/ELSE statements). You can use the MPRINT system option to see the generated code in the log:
414 options mprint; 415 416 data want; 417 set have; 418 %recode(9,11,HIghBP DIABetes CANCeR HEARTd STROKe LUNG) MPRINT(RECODE): highbp9 = (R9HIBP=1 | R9HIBPE=1); MPRINT(RECODE): diabetes9 = (R9DIAB=1 | R9DIABE=1); MPRINT(RECODE): cancer9 = (R9CANCR=1 | R9CANCRE=1); MPRINT(RECODE): heartd9 = (R9HEART=1 | R9HEARTE=1); MPRINT(RECODE): stroke9 = (R9STROK=1 | R9STROKE=1); MPRINT(RECODE): lung9 = (R9LUNG=1 | R9LUNGE=1); MPRINT(RECODE): highbp10 = (R10HIBP=1 | R10HIBPE=1); MPRINT(RECODE): diabetes10 = (R10DIAB=1 | R10DIABE=1); MPRINT(RECODE): cancer10 = (R10CANCR=1 | R10CANCRE=1); MPRINT(RECODE): heartd10 = (R10HEART=1 | R10HEARTE=1); MPRINT(RECODE): stroke10 = (R10STROK=1 | R10STROKE=1); MPRINT(RECODE): lung10 = (R10LUNG=1 | R10LUNGE=1); MPRINT(RECODE): highbp11 = (R11HIBP=1 | R11HIBPE=1); MPRINT(RECODE): diabetes11 = (R11DIAB=1 | R11DIABE=1); MPRINT(RECODE): cancer11 = (R11CANCR=1 | R11CANCRE=1); MPRINT(RECODE): heartd11 = (R11HEART=1 | R11HEARTE=1); MPRINT(RECODE): stroke11 = (R11STROK=1 | R11STROKE=1); MPRINT(RECODE): lung11 = (R11LUNG=1 | R11LUNGE=1); 419 run;
I agree with Reeza that a "long" data structure would likely simplify your code even more than just creating all these variable names in macro loops.
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.