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

Hello programmers,

 

I have 3 variables (COPD, EMPHY and BRONCH) with values 0,1 and . (missing) that i want to use to make a new variable COPDix with 0,1 and . 

 

I want if COPD=1 or EMPHY=1 or BRONCH=1 then COPDix=1;

and if COPD=. or EMPHY=. or BRONCH=. then COPDix=.;

every other thing should be zero.

 

I did something like this:

Data ACBS2; set ACBS1;
if COPD= '.' then COPDix=.;
if Bronch= '.' then COPDix=.;
if Emphy= '.' then COPDix=.;
if COPD= '1' then COPDix=1;
else if Bronch= '1' then COPDix=1;
else if Emphy= '1' then COPDix=1;
else COPDix=0;
run;

This code implements only COPD=1 or EMPHY=1 or Bronch=1 then COPDix=1;

However it doesn't implement the second idea: It calls both . and 0 in COPD, EMPHY and BRONCH as 0 in COPDix. 

 

I know i'm missing a little bit of something to make this work. Any help will be appreciated.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Do you really have character variables now as the code is showing or numeric variables as your problem statement is showing?    If you do have character variables why are you making the new variable numeric instead of character?

 

Why not just code the statement as you have written it?

if COPD=1 or EMPHY=1 or BRONCH=1 then COPDix=1;
else if COPD=. or EMPHY=. or BRONCH=. then COPDix=.;
else COPDix=0;

 

 

 

 

View solution in original post

6 REPLIES 6
ChuksManuel
Pyrite | Level 9
sorry i meant:

I want if COPD=1 or EMPHY=1 or BRONCH=1 then COPDix=1;

and if COPD=. AND EMPHY=. AND BRONCH=. then COPDix=.;
Tom
Super User Tom
Super User

If you are unclear what result you want it might help to build a truth table.

Show for all combinations of the possible values of the three input values what output you want. 

 

Since you appear to be using tri-level logic instead of binary logic there will by 3*3*3 = 27 possible combination of the three input variables.

Tom
Super User Tom
Super User

Do you really have character variables now as the code is showing or numeric variables as your problem statement is showing?    If you do have character variables why are you making the new variable numeric instead of character?

 

Why not just code the statement as you have written it?

if COPD=1 or EMPHY=1 or BRONCH=1 then COPDix=1;
else if COPD=. or EMPHY=. or BRONCH=. then COPDix=.;
else COPDix=0;

 

 

 

 

ChuksManuel
Pyrite | Level 9
Hi Tom,
The second case is
if COPD=. AND EMPHY=. AND BRONCH=. then COPDix=.;
Tom
Super User Tom
Super User

@ChuksManuel wrote:
Hi Tom,
The second case is
if COPD=. AND EMPHY=. AND BRONCH=. then COPDix=.;

So just change it.  

Does it work or not?

If not show an example set of inputs that it is not producing the right answer for.

Tom
Super User Tom
Super User

I suspect you just want to use the MAX() function.

Let's do a truth table for two inputs and show the result of the MAX() function and you can compare that to what you want.  It should then work the same for 3 or more variables.

Here are all 9 (3*3) possible combinations.

A  B MAX(A,B)
0  0  0
0  1  1
1  0  1
1  1  1
.  .  .
.  0  0
.  1  1
0  .  0
1  .  1 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 6 replies
  • 616 views
  • 2 likes
  • 2 in conversation