BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
lillymaginta
Lapis Lazuli | Level 10

I have the following code: 

data a (drop=i);

  set b;

  array x{*} dx:;

hf=0;

do i=1 to dim(x);

hf=ifn(substrn(x{i}, 1, 3) in('428', 'I43'),1,hf);

end;

run;

 

data a (drop=i);

  set b;

  array x{*} dx:;

hf=0;

do i=1 to dim(x);

hf=ifn(substrn(x{i}, 1, 4) in('d282', 'd432'),1,hf);

end;

run;

 

Is there is a way to combine the two codes into one code given that I am requiring different lengths in the substrn statement, (For example 3 in the first and 4 in the second). 

I am looking to get a code that would be something similar to: 

 

 

hf=ifn(substrn(x{i}, 1, 3) in('428', 'I43'),1,hf) or hf=ifn(substrn(x{i}, 1, 4) in('d282', 'd432'),1,hf);

Is it possible to have such statement? 

Thanks

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

Not sure what you are trying to do but, if i do understand correctly, the following should work:

data b;
  input (dx1-dx5) ($);
  datalines;
d234 d463 d213 d282 d435
d213 428 d360 d145 d269
d409 d231 I43 d690 d432
234 463 213 678 435
213 428 360 145 269
409 231 I43 690 609
;
data a (drop=i);
  set b;
  array x{*} dx:;
  hf=0;
  do i=1 to dim(x);
    hf=ifn(substrn(x{i}, 1, 3) in('428', 'I43')
      or
           substrn(x{i}, 1, 4) in('d282', 'd432'),
    1,hf);
  end;
run;

Art, CEO, AnalystFinder.com

 

View solution in original post

7 REPLIES 7
VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

have you tried:

 

if hf=ifn(substrn(x{i}, 1, 3) in('428', 'I43'),1,hf);

else if hf=ifn(substrn(x{i}, 1, 4) in('d282', 'd432'),1,hf);

lillymaginta
Lapis Lazuli | Level 10

Thank you VDD, the statement is not correct and would generate an error (no matching if-then clause) 

lillymaginta
Lapis Lazuli | Level 10

Thank you for sharing the link. If I understand correctly, they did not use the else statement with "ifn" so I am not sure it is applicable here. Maybe one of the advisors can help (@RW9) 

art297
Opal | Level 21

Not sure what you are trying to do but, if i do understand correctly, the following should work:

data b;
  input (dx1-dx5) ($);
  datalines;
d234 d463 d213 d282 d435
d213 428 d360 d145 d269
d409 d231 I43 d690 d432
234 463 213 678 435
213 428 360 145 269
409 231 I43 690 609
;
data a (drop=i);
  set b;
  array x{*} dx:;
  hf=0;
  do i=1 to dim(x);
    hf=ifn(substrn(x{i}, 1, 3) in('428', 'I43')
      or
           substrn(x{i}, 1, 4) in('d282', 'd432'),
    1,hf);
  end;
run;

Art, CEO, AnalystFinder.com

 

data_null__
Jade | Level 19

Seems like you're making this too hard.  It would be helpful to see your input data.  I borrowed from @art297 please indicate if that assumption is correct.

 

data b;
   id + 1;
   array dx[5] $8; 
   input dx[*];
   array _hf[4] $8 _temporary_ ('428' 'I43' 'd282' 'd432');
   array _st[2] $8 _temporary_ ('d269' '428');
   hf=0; st=0; 
   do i = 1 to dim(dx);;
      hf + dx[i] in _hf);
      st + dx[i] in _st);
      end;
   hf = hf gt 0;
   st = st gt 0;
   drop i;
   label hf='Heart Failure' st='Stroke';
   datalines;
d234 d463 d213 d282 d435
d213 428 d360 d145 d269
d409 d231 I43 d690 d432
234 463 213 678 435
213 428 360 145 269
409 231 I43 690 609
;;;;
   run;
proc print;
   run;

Capture.PNG

 

lillymaginta
Lapis Lazuli | Level 10

Thank you! 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 7 replies
  • 1626 views
  • 4 likes
  • 4 in conversation