BookmarkSubscribeRSS Feed
inquistive
Quartz | Level 8

Hello everyone, 

I am looking for a condensed way to write the following (repetitive and hard coded) block of code. Is there a way to do so in macro or something else? Any help would be greatly appreciated.

case when gender = 'M'  then A  else ' ' end) as MA,

case when gender = 'M’  then B else . end) as MB,

 case when gender = 'M'  then C else . end) as MC,

 

case when gender = 'F'  then A else ' ' end) as FA,

case when gender = 'F'  then B else . end) as FB,

case when gender = 'F'  then C else . end) as FC

(and so on...)

FROM student;

3 REPLIES 3
Reeza
Super User

A data step is probably more efficient for starters. Can you explain a bit more about what you're trying to do here overall?
Not sure a one liner is possible if you're creating multiple variables. This looks possibly like a transpose though, which may be better in PROC TRANSPOSE.

 


@inquistive wrote:

Hello everyone, 

I am looking for a condensed way to write the following (repetitive and hard coded) block of code. Is there a way to do so in macro or something else? Any help would be greatly appreciated.

case when gender = 'M'  then A  else ' ' end) as MA,

case when gender = 'M’  then B else . end) as MB,

 case when gender = 'M'  then C else . end) as MC,

 

case when gender = 'F'  then A else ' ' end) as FA,

case when gender = 'F'  then B else . end) as FB,

case when gender = 'F'  then C else . end) as FC

(and so on...)

FROM student;


 

inquistive
Quartz | Level 8

This is just a part of a larger query. In this part I am trying to grouping the data for easier analysis. 

Reeza
Super User

Then I suspect the answer is no, based on what you've shown so far. I strongly suspect there's other ways to get what you need but SQL isn't particularly great at wrangling data efficiently across many variables sometimes. 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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