BookmarkSubscribeRSS Feed
sahoositaram555
Pyrite | Level 9

Hi Experts,

I've a dataset containing a column named Category like below

Category

1

1

1

1

1

2

2

2

2

2

3

3

3

4

4

4

4

4

;

I would like to create a macro that gives me output with its count like below

1- 5

2-5

3-4

4-5

;

 

To do this i have taken

%macro cat(); 

%let Received_data = %sysfunc(open(dataset.p1));
%If &Received_data %Then %Do;
Proc freq data=dataset.p1;
Table &col./out=outfile;
run;
%End;

%mend cat();

Later from this step I'm trying to put it in to call symputx(). 

 

If anyone knows a better method to complete this please do help with your response.

 

5 REPLIES 5
Kurt_Bremser
Super User

This is simple summarizing of data, so you don't need a macro:

data want;
set have;
by category;
if first.category
then count = 1;
else count + 1;
if last.category;
result = catx('-',category,count);
keep result;
run;
sahoositaram555
Pyrite | Level 9
Hi @Kurt_Bremser,
Thank you for your response. Though this can be done by the method you have shown and i could see that its working, but for my further analysis depends on this which for which i would like to macrotize all the individual observations with it's count assigned to individual values.
Therefore, I would be grateful if you can guide me with your kind response .
sahoositaram555
Pyrite | Level 9
I will pass them as arguments in to models which i'm building.
Kurt_Bremser
Super User

So you would have a macro wrapper around your code:

%macro run_single(param);
/* your model code here, where &param would be one of the instances from the summarization */
%mend;

which you can use from the resulting dataset of my previous code:

data _null_;
set want;
call execute('%nrstr(%run_single(' !! result !! '))');
run;
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
  • 5 replies
  • 1476 views
  • 0 likes
  • 2 in conversation