SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
GN0001
Barite | Level 11

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

Blue Blue
15 REPLIES 15
PaigeMiller
Diamond | Level 26

I don't really understand what you are trying to do.


Can you explain in words? Can you give a small example?

--
Paige Miller
Reeza
Super User
You're trying to check if the fields are sorted in order of ascending or descending?
GN0001
Barite | Level 11
How can I check?
So would that be;
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

Regards.
Blueblue
Blue Blue
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
GN0001
Barite | Level 11

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

Blue Blue
Reeza
Super User
Your title and code/comments do not align. Are you trying to nest your CASE statements? It would be better if you explained what you were trying to do.
GN0001
Barite | Level 11

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

 

Blue Blue
Reeza
Super User
Unfortunately this still doesn't make sense, especially since CASE statements are a SQL concept and IF/ELSE is a data step so not sure how you would nest those? Typically a CASE statement is the IF/ELSE from a data step.

I think it's time you illustrate your problem with some example data.


GN0001
Barite | Level 11

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

  

Blue Blue
Reeza
Super User
It's structured correctly then, so what is the issue? Did your code like that not work?
GN0001
Barite | Level 11
I am working on it to see who to write. I am thinking about the logic.
Thanks,
blueblue
Blue Blue
GN0001
Barite | Level 11

Yes, I am trying to nest two case statement.

Regards,

blueblue

Blue Blue
Reeza
Super User

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;
GN0001
Barite | Level 11

Please give me some time to test it.

Regards,

blueblue

Blue Blue

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 15 replies
  • 2768 views
  • 7 likes
  • 3 in conversation