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

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