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;
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;
This is just a part of a larger query. In this part I am trying to grouping the data for easier analysis.
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.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.