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

Hello

I have a small code for creating a indicator variable. But the code is conflicting with each other and so one of the statements is not been read by SAS, and I do not know of another way to write it.

My code can be seen below:

Data want;

Length Colour $6;

Set Have;

If Performance_2014 <0 And Performance_2013 <0 And Performance_2012 <0 And Performance_2011 <0 then indikator = 1;

Else indikator = 3;

If Performance_2014 <0 And Performance_2013 <0 And Performance_2012 <0 then indikator = 2;

run;

If all 4 years are negative, the indicator still gives the value "2" because of the second statement, even though it should give "1", .

Is there anyway to ask SAS to first check if all 4 years have a negative value and if this is not true, then check if the last 3 years are negative?

I managed to get this to work in excel, but not in SAS. In excel it would look like this:

If(and(C2<0;D2<0;E2<0;F2<0);"0";If(and(D2<0;E2<0;F2<0);"1";"3"))

Thank you for your time and help

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Just make sure to use proper IF/THEN/ELSE nesting.

If Performance_2014 <0 And Performance_2013 <0 And Performance_2012 <0 And Performance_2011 <0 then indikator = 1;

ELSE If Performance_2014 <0 And Performance_2013 <0 And Performance_2012 <0 then indikator = 2;

Else indikator = 3;

View solution in original post

3 REPLIES 3
Tom
Super User Tom
Super User

Just make sure to use proper IF/THEN/ELSE nesting.

If Performance_2014 <0 And Performance_2013 <0 And Performance_2012 <0 And Performance_2011 <0 then indikator = 1;

ELSE If Performance_2014 <0 And Performance_2013 <0 And Performance_2012 <0 then indikator = 2;

Else indikator = 3;

CTorres
Quartz | Level 8

Try this:

If Performance_2014 <0 And Performance_2013 <0 And Performance_2012 <0 And Performance_2011 <0 then indikator = 1;

Else If Performance_2014 <0 And Performance_2013 <0 And Performance_2012 <0 then indikator = 2;

Else indikator = 3;

You could also try the IFN Function.

Regards,

Bruce123
Calcite | Level 5

Thank you both very much for your help.

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
  • 3 replies
  • 760 views
  • 3 likes
  • 3 in conversation