Hello guys,
I know it's probably an easy code to write but i want to be corrected on this.
I want to create a variable heartvar6 , such that heartvar6=1 if a participant has 1 or more heart failures (eg any of heartvar1-heartvar5 = 1). I also want to call heartvar6 as missing if the patient has a combination of 0's and missings . And then 0 otherwise.
For example, in the dataset i posted, i want observation 3 to be missing but observation 5 will be 0.
data one;
input heart1 heart2 heart3 heart4 heart5;
datalines;
1 0 0 0 0
0 1 . .0
0 0 0 . 0
1 1 0 1 0
0 0 0 0 0
; run;
data two; set one;
if heartvar1 =1 or heartvar2=1 or heartvar3=1 or heartvar4=1 or heartvar5=1 then heartvar6=1;
else if heartvar1=0 and heartvar2=0 and heartvar3=0 and heartvar4=0 and heartvar5=0 then heartvar6=0;
else heartvar6=.;
proc print; run;
please can i use an array for this?
This code gave me the logic that i wanted to implement. is there a more efficient way of writing this?
Thanks
Use of Array simplifies the program but must know that the use of double ^ before an expression gets either 1 or 0.
data one; input heart1 heart2 heart3 heart4 heart5; datalines; 1 0 0 0 0 0 1 . . 0 0 0 0 . 0 1 1 0 1 0 0 0 0 0 0 ; run; data two; set one; array h heart1 - heart5; heart6 = ^^whichN(1, of h[*]); if heart6 = 0 and nmiss(of h[*]) then heart6 = .; run;
Use of Array simplifies the program but must know that the use of double ^ before an expression gets either 1 or 0.
data one; input heart1 heart2 heart3 heart4 heart5; datalines; 1 0 0 0 0 0 1 . . 0 0 0 0 . 0 1 1 0 1 0 0 0 0 0 0 ; run; data two; set one; array h heart1 - heart5; heart6 = ^^whichN(1, of h[*]); if heart6 = 0 and nmiss(of h[*]) then heart6 = .; run;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.