Hello team,
I want to write two nested case statements:
case1
field1 > field2 and field2> field3 and field3 >field4 then
case1
case2
case3
case2 field1 < field2 and field2< field3 and field3 <field4 then
case1
case2
case3
What would be the syntax? Or is this doable?
Regards,
Blue blue
I don't really understand what you are trying to do.
Can you explain in words? Can you give a small example?
If field1 > field2 then
When case ….then,,,
When case….then…
End as ‘this’Else
When case.,,,then….
When case….then.,.End as ‘that’
End if
I still don't understand the question.
Hello,
Please refer to this:
You're trying to check if the fields are sorted in order of ascending or descending?
Yes, I am trying to see how I need to write it.
Regards,
blue blue
Hello all,
yes, I need to nest my case statement within another case statement of within an if else statement:
but I have to say if the sort is like field1 > field 2, then
else somethingelse
end;
But within each part of if else we have case statements.
I hope this makes sense.
Thanks,
blueblue
Reeza,
Thanks for the response.
This is the sketch of what I have in mind.
case
When (field1>field2 and field2>field3) Then
case
when this then than /*this is nested within the first case*/
when this then than /*this is nested within the first case*/
end
When (field1<field2 and field2<field3) Then
case
when this then than /*this is nested within the first case*/
when this then than /*this is nested within the first case*/
end
end
Thanks for the response.
blueblue
Yes, I am trying to nest two case statement.
Regards,
blueblue
Entirely useless example:
proc sql;
create table example as
select *, case when age <14 then (case when sex = 'F' then "FEMALE" else "MALE" end)
when age >=14 then (case when sex = 'F' then "Female" else "Male" end) end as gender
from sashelp.class;
quit;
Please give me some time to test it.
Regards,
blueblue
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.