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!all

3 REPLIES 3
Ksharp
Super User

No need for so many proc sql block, only one is enough . and combine these sql statement which are under the same condition.

proc sql;

........

.......

select count(*),max(count),count (distinct name)

into : count1, :count2, :cnt

from dataset1

where count>=5

;

............

............

quit;

jwillis
Quartz | Level 8

Adding to your code:

%macro choose(type=);

proc sql;

........

.......

select count(*),max(count),count (distinct name)

into : count1&type., :count2&type., :cnt&type.

from dataset1

%if &type = eq %then %do;

where count = 5

%end;

%if &type = lt %then %do;

where count < 5

%end;

%if &type = gt %then %do;

where count > 5

%end;

;

quit;

%mend choose;

%choose(type=eq);

%choose(type=lt);

%choose(type=gt);

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

IMO the answer to:

" I am not sure whether this is the most efficient way."


would be no.  Whenever I need to deal with more than one (or even one if its dates or such like) then I would put parameters into a dataset.  It is then simply a matter of merging on the parameter dataset to use them.  Avoids all that nasty:

&&&%str(&&&av_&&i.) nonsense with simple to understand merging.


Most languages provide some sort of macro facility, but they are used very rarely and only for specific things.  The opposite is true in SAS, most of the code seems to be in macros even though with a bit of thought 90% of it can be done in straight code.

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
  • 1632 views
  • 0 likes
  • 4 in conversation