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

Hello,

 

I am wondering if someone could help me with an issue I am having.

 

I am currently trying to find multiple outcomes for one observation across multiple scenarios, however i seem to be getting a repeat answer for my solution. I have seen multiple outcomes are possible i am not sure on how to get these to be outputted in one step. I believe i may have over complicated this, or it may not be possible.

 

I have provided the an example code below:

 

data original;

input KEY $ Number Type $ Code $;

/*id=_n_;*/

datalines;

1 5 Car Fast
2 100 Foot Slow
3 10 Bus Fast
4 25 Foot Medium
5 60 Car Slow

;

run;

 

My current code:

 


DATA ORIGINAL2; SET ORIGINAL;
LENGTH OUTCOME1 OUTCOME2 OUTCOME3 OUTCOME4 $20.;

ARRAY OUTCOME [*] $ OUTCOME1 - OUTCOME9;

DO i = 1 TO 5;
IF OUTCOME[i] = "" THEN DO;

IF OUTCOME1 NE "1" OR OUTCOME2 NE "1" OR
OUTCOME3 NE "1" OR OUTCOME4 NE "1" THEN DO;

IF Number <55 AND Type = "Foot" THEN DO;
OUTCOME[i] = "1";
END;
END;
END;

IF OUTCOME[i] = "" THEN DO;

IF OUTCOME1 NE "2" OR OUTCOME2 NE "2" OR
OUTCOME3 NE "2" OR OUTCOME4 NE "2" THEN DO;

IF Type = "Foot" and Code = "Slow" THEN DO;
OUTCOME[i] = "2";
END;
END;
END;

IF OUTCOME[i] = "" THEN DO;

IF OUTCOME1 NE "3" OR OUTCOME2 NE "3" OR
OUTCOME3 NE "3" OR OUTCOME4 NE "3" THEN DO;

IF Code = "Slow" THEN DO;
OUTCOME[i] = "3";
END;
END;
END;

IF OUTCOME[i] = "" THEN DO;

IF OUTCOME1 NE "4" OR OUTCOME2 NE "4" OR
OUTCOME3 NE "4" OR OUTCOME4 NE "4" THEN DO;

IF Number > 5 THEN DO;
OUTCOME[i] = "4";
END;
END;
END;
END;


RUN;

 

You will notice that the outcome repeats its self until loop 5 where this changes.

 

My desired outcome is this:

 

data Outcome;
input KEY $ Number Type $ Code $ OUTCOME1 $ OUTCOME2 $ OUTCOME3 $ OUTCOME4 $;

/*id=_n_;*/

datalines;

1 5 Car Fast . . . .
2 100 Foot Slow 2 3 4 .
3 10 Bus Fast 4 . . .
4 25 Foot Medium 1 4 . .
5 60 Car Slow 3 4 . .

;

run;

 

Does anybody know how to do this as i am getting confused. I have noticed that when I remove the  IF OUTCOME1 NE "4" OR OUTCOME... section this constantly repeats it as at point 5 i can see this actually changes currently.

 

Thank you,

Michael 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User
data original2;
set original;
length cresult $4;
array outcome{*} $4 outcome1-outcome4;
if Number < 55 and Type = "Foot" then cresult = "1";
if Type = "Foot" and Code = "Slow" then cresult = cats(cresult,"2");
if Code = "Slow" then cresult = cats(cresult,"3");
if Number > 5 then cresult = cats(cresult,"4");
do i = 1 to length(cresult);
  select (substr(cresult,i,1));
    when ("1") outcome{i} = "ONE1";
    when ("2") outcome{i} = "TWO2";
    when ("3") outcome{i} = "THRE";
    when ("4") outcome{i} = "FOUR";
    otherwise;
  end;
end;
drop cresult i;
run;

View solution in original post

8 REPLIES 8
Kurt_Bremser
Super User

Please explain the rules for building your 4 result variables in plain language.

Why will they all be missing in the first observation, and how are the values in the other observations determined?

Mick_bill
Fluorite | Level 6

Sorry.

 

I am looking to how many of the rules in the outcomes are being hit. So i have label them as outcome to show which one they have hit.

The first one obs does not hit any of the outcomes as it doesn't hit any of the conditions stated. But after this we see that each observation hits at least one of the conditions. If it hits multiple conditions I would need it state each of the conditions it has hit. The ordering would need to be which outcome was hit first at the start and then follow with the other outcomes as it goes down the conditions. So i have labelled it outcome 1 to 4 to show which of these outcomes where hit first and then which had then followed.

Which is what outcome dataset it trying to show.

thank you.

Mick_bill
Fluorite | Level 6

 

The conditions are :

IF Number <55 AND Type = "Foot"  = 1

IF Type = "Foot" and Code = "Slow" = 2

IF Code = "Slow" = 3

IF Number > 5  = 4

The final result are if it hit ones of the rules or multipleThe outcome is just the name of the variable. This could be named anything as long as it is number 1 to 4.

So an account may hit no conditions, one condition or multiple conditions. Depending what it hits then depends what is populated in the outcome variable.

 

thanks.

 

Kurt_Bremser
Super User

See this:

data original2;
set original;
length cresult $4;
array outcome{*} outcome1-outcome4;
if Number < 55 and Type = "Foot" then cresult = "1";
if Type = "Foot" and Code = "Slow" then cresult = cats(cresult,"2");
if Code = "Slow" then cresult = cats(cresult,"3");
if Number > 5 then cresult = cats(cresult,"4");
do i = 1 to length(cresult);
  outcome{i} = input(substr(cresult,i,1),1.);
end;
drop cresult i;
run;
Mick_bill
Fluorite | Level 6
thanks for this. Would this be possible if the outcomes where not numbers but a character variable which is length of 4 E.g. instead of 1, 2 3 4
It is
ONE1
TWO2
THRE
FOUR
Kurt_Bremser
Super User
data original2;
set original;
length cresult $4;
array outcome{*} $4 outcome1-outcome4;
if Number < 55 and Type = "Foot" then cresult = "1";
if Type = "Foot" and Code = "Slow" then cresult = cats(cresult,"2");
if Code = "Slow" then cresult = cats(cresult,"3");
if Number > 5 then cresult = cats(cresult,"4");
do i = 1 to length(cresult);
  select (substr(cresult,i,1));
    when ("1") outcome{i} = "ONE1";
    when ("2") outcome{i} = "TWO2";
    when ("3") outcome{i} = "THRE";
    when ("4") outcome{i} = "FOUR";
    otherwise;
  end;
end;
drop cresult i;
run;
ballardw
Super User

@Mick_bill wrote:

 

The conditions are :

IF Number <55 AND Type = "Foot"  = 1

IF Type = "Foot" and Code = "Slow" = 2

IF Code = "Slow" = 3

IF Number > 5  = 4

The final result are if it hit ones of the rules or multipleThe outcome is just the name of the variable. This could be named anything as long as it is number 1 to 4.

So an account may hit no conditions, one condition or multiple conditions. Depending what it hits then depends what is populated in the outcome variable.

 

thanks.

 


So what do you want when Number < 55 , Type='Foot' and Code='Slow'? If you want the 1 then you need to think in terms of ELSE

IF Number <55 AND Type = "Foot"  then Variable  = 1;
Else IF Type = "Foot" and Code = "Slow"  then Variable = 2;
Else IF Code = "Slow" = 3 then Variable=3;
Else IF Number > 5  then Variable = 4 ;

Otherwise regardless of the value of Type or Code when Number > 5 you will get a result of 4.

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
  • 8 replies
  • 616 views
  • 0 likes
  • 3 in conversation