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. 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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