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

 i have a database that looks like this 

iddiag 1 diag2  diag3diag4diag5diag7diag8diag9
110110000
200000000
300 00 11
4 0000000

 

I need to create a new_var that is equals to 1 if either one of the 'diag's  have a value of 1.

I did it by summing the diags and having the new_var = 1 if sum(diag1-diag24)>=1.

Is there another way to do this without typing a lot? Also, my technique only works if the observantions are 1s and 0s. I could do something like this if my obs values were different than 1 --> diag1=2 or diag2=1 or diag3=5....and so on then new_var=1. This isn't very practical when i have many 'diag's, so how could I do this without typing a lot?

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Finding only a 1 (not a 2 or a 5) can be done with only mild complications.  The CAT family of functions works on numeric as well as character values.  So you could code:

 

if index(cats(of diag1-diag24), '1') > 0;

 

If your data contains two-digit numbers (so you need to find "1" but not "10"), it can still be done but gets mildly more complex.

View solution in original post

5 REPLIES 5
PeterClemmensen
Tourmaline | Level 20

I think your solution is solid, though you would probably type sum(of diag1-diag24)>=1 🙂

K_S
Obsidian | Level 7 K_S
Obsidian | Level 7

good catch! thanks 😉 

Astounding
PROC Star

Finding only a 1 (not a 2 or a 5) can be done with only mild complications.  The CAT family of functions works on numeric as well as character values.  So you could code:

 

if index(cats(of diag1-diag24), '1') > 0;

 

If your data contains two-digit numbers (so you need to find "1" but not "10"), it can still be done but gets mildly more complex.

ballardw
Super User

For a very few character shorter typing:

 

new_var = (index(catt(of diag:),'1')>0);

Vish33
Lapis Lazuli | Level 10

sum(of diag1-diag9) >=1 OR max(of diag1-diag9) >=1 should work.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 794 views
  • 4 likes
  • 5 in conversation