BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ChuksManuel
Pyrite | Level 9

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 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
KachiM
Rhodochrosite | Level 12

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;

View solution in original post

1 REPLY 1
KachiM
Rhodochrosite | Level 12

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;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 487 views
  • 1 like
  • 2 in conversation