BookmarkSubscribeRSS Feed
sasmaverick
Obsidian | Level 7

I am trying to create multiple macro variables based on various conditions. Below is my code. I am not sure whether this is the most efficient way.

*to get total number of obs and max value of count variable where count<5;

proc sql noprint;

select count(*),max(count)

into : count1, count2

from dataset1

where count<5

;quit;

*to get total number of obs and max value of count variable where count>=5;

proc sql noprint;

select count(*),max(count)

into : count1, count2

from dataset1

where count>=5

;quit;

*count of name where count>=5;

proc sql;

select count (distinct name) into:cnt

from dataset1

where count>=5;

quit;

*count of name where count<5;

proc sql;

select count (distinct name) into:cnt

from dataset1

where count<5;

quit;

*get names into macro where count<5;

proc sql

select distinct name into : name1-:name&sysmaxlong

from dataset1

where count<5;

quit;

repeat the above step for count>=5


Is there a shorter/more concise way for the above. Will appreciate inputs on a more efficient approach.


Thanks!

1 REPLY 1
LinusH
Tourmaline | Level 20

Are you worried about performance?

It seems that most of these SQL can be merged into one query, by using CASE in the select clause, instead of using a where clause.

Data never sleeps

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 1 reply
  • 842 views
  • 0 likes
  • 2 in conversation