outputing different data sets in a data sets within a macro

Reply
New Contributor
Posts: 2

outputing different data sets in a data sets within a macro

Hi there,

I want to output different data sets within a macro depending on the number categories of the categorical variable in use. For example, let’s say if I have the variable income with 4 categories[-, 50 000),(50 000, 80 000],(80 000, 120 000], (120 000, +0).

The code I have for this is the following:

data data_50 data_50to80 data._80to120 data120;

set dataX;

        if         &var = '[-, 50 000)' then output data_50;

          else if &var = '(50 000, 80 000]' then output data_50to80;

          else if &var = '(80 000, 120 000]' then output data_80to120;

          else if &var = '(120 000, +]' then output data_120;

drop &var;

run;

where &var is a macro variable I created for the macro (&var=income). I want to be able to perform the previous data step with other categorical variables with different number categories. For example: score (low, medium, high). After I have those data sets, I want to be able to transpose them each one of them, and then ‘set’ them in a subsequent data set.

Could you please suggest me how to do so?

Thanks a lot

.

Esteemed Advisor
Esteemed Advisor
Posts: 7,253

Re: outputing different data sets in a data sets within a macro

I would suggest that its not a good idea to duplicate your data over and over again just to add categories, create category variables in the one dataset. In the example you have given, you can create a format, then apply that format into a new variable:

proc format;
  value grp1
    0-50000="data_50"
    50000-80000="data_80"
...
;
run;

data want;
  length cat1 $20;
  cat1=put(yourval,grp1.);
run;

There are lots of little tricks like this using formats.  So create one dataset, with different categories cat1 cat2 etc. for each categorisation.  Then you can where or by group based on them.

 

New Contributor
Posts: 2

Re: outputing different data sets in a data sets within a macro

Thanks a lot.
Ask a Question
Discussion stats
  • 2 replies
  • 102 views
  • 0 likes
  • 2 in conversation