0

I can create multiple data sets using the following way:

data D3RGDPX  (drop=BasePeriod BaseYear Forecast10Year)
 D3GDPX   (drop=BasePeriod BaseYear Forecast10Year)
 D3BFIX   (drop=BasePeriod BaseYear Forecast10Year)
 D3CPAT   (drop=BasePeriod BaseYear Forecast10Year)
 D3IP     (drop=BasePeriod BaseYear Forecast10Year)
 D3TPHS   (drop=BasePeriod BaseYear Forecast10Year)
 D3PPI    (drop=BasePeriod BaseYear Forecast10Year)
 D3CPI    (drop=BasePeriod BaseYear Forecast10Year)
 D3UNPR   (drop=BasePeriod BaseYear Forecast10Year) 
 D3WMFG   (drop=BasePeriod BaseYear Forecast10Year)
 D3RTTR   (drop=BasePeriod BaseYear Forecast10Year)
 D3AUTODF (drop=BasePeriod BaseYear Forecast10Year)
 D3SPIF   (drop=BasePeriod BaseYear Forecast10Year);
 set my.data;
     if      Type='RGDPX'  then output D3RGDPX;
     else if Type='GDPX'   then output D3GDPX;
     else if Type='BFIX'   then output D3BFIX;
     else if Type='CPAT'   then output D3CPAT;
     else if Type='IP'     then output D3IP;
     else if Type='TPHS'   then output D3TPHS;
     else if Type='PPI'    then output D3PPI;
     else if Type='CPI'    then output D3CPI;
     else if Type='UNPR'   then output D3UNPR;
     else if Type='WMFG'   then output D3WMFG;
     else if Type='RTTR'   then output D3RTTR;
     else if Type='AUTODF' then output D3AUTODF;
     else if Type='SPIF'   then output D3SPIF;
run;

I tried to do this within a macro, but each time the macro would overwrite the other such that only the last data set would remain and it wouldn't create multiple data sets. How can I accomplish this task within a macro?

Added from comments:

%macro new (data=, set=, Type=); 
data &data (drop=BasePeriod BaseYear Forecast10Year); 
set my.data; 
if Type = "&Type" then output &data; 
run; 
%mend new;
 %new (data =D3RGDPX, Type = RGDPX); 
%new (data=D3GDPX, Type=GDPX);

And so on.

Joe
  • 62,789
  • 6
  • 49
  • 67
mustafghan
  • 169
  • 4
  • 15
  • Please show us the code you have attempted so far. – DomPazz Aug 14 '15 at 16:57
  • %macro new (data=, set=, Type=); data &data (drop=BasePeriod BaseYear Forecast10Year); set my.data; if Type = "&Type" then output &data; run; %mend new; %new (data =D3RGDPX, Type = RGDPX); %new (data=D3GDPX, Type=GDPX); And so on. – mustafghan Aug 14 '15 at 16:59
  • I put your code into the question -- it is easier to read there. – DomPazz Aug 14 '15 at 17:18
  • I think you should rewrite the question to say what you're actually trying to do (why are you doing a macro for this, what are you pulling in general). May be an XY problem. – Joe Aug 14 '15 at 17:27
  • @Joe I am basically trying to create a series of data sets based on the conditions I've set. So there would be one data set when the Type is RGDPX, one when it's GDPX, etc. – mustafghan Aug 15 '15 at 21:02

1 Answers1

0

I don't see why your macro creates the problem you describe. If you want a single macro to run this, try something like

%macro outds(types=, input=);
%local i n type;
%let n = %sysfunc(countw(&types));
data 
%do i=1 %to &n;
    %let type=%scan(&types,&i);
    D3&type (drop=BasePeriod BaseYear Forecast10Year)
%end;
;
set &input;

%do i=1 %to &n;
    %let type=%scan(&types,&i);
    if type="&type" then output D3&type;
%end;
run;
%mend;

options mprint;
%outds(types=RGDPX GDPX BFIX CPAT IP TPHS PPI CPI UNPR WMFG RTTR AUTODF SPIF,
       input=my.data);

This builds a Data Step like your example taking an list of types.

DomPazz
  • 12,415
  • 17
  • 23