BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
bpearce
Calcite | Level 5

Hi,

I have called the macro-variable YR below.  If YR= 2014 then I would like to execute my only the first data step.  If YR = 2015 then I would like to execute only the 2nd data step. How do I accomplish this?  I'm using EG 7.1. Much appreciated!

 

%let YR=2014;

 

data SKBusiness2014;

set MU2014.final_&yr.

(keep= clt_ID snm gvn_nm bus_inc_gross bus_inc_net prof_inc_gross prof_inc_net tax_prov prov_of_res mj_net_inc_aloc_sk);

where (tax_prov = 7 OR (tax_prov = 13 and mj_net_inc_aloc_sk > 0)) AND

(bus_inc_net ne 0 or

bus_inc_gross ne 0 or

prof_inc_net ne 0 or

prof_inc_gross ne 0)

;

format prov_of_res prov_of_resfmt.

tax_prov tax_provfmt.;

run;

 

 

 

data SKBusiness2015;

set MU2015.w48_cumulated_2015;

(keep= clt_ID snm gvn_nm bus_inc_gross bus_inc_net prof_inc_gross prof_inc_net tax_prov prov_of_res mj_net_inc_aloc_sk);

where (tax_prov = 7 OR (tax_prov = 13 and mj_net_inc_aloc_sk > 0)) AND

(bus_inc_net ne 0 or

bus_inc_gross ne 0 or

prof_inc_net ne 0 or

prof_inc_gross ne 0)

;

format prov_of_res prov_of_resfmt.

tax_prov tax_provfmt.;

run;

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

The differences seem simply enough that you might not need to write a macro.  Most of the differences you can use the value of the YR macro variable to replace. The other main difference looks to be that the name of the input data set is different.  You could use the IFC() function to select between two choices based on the value of YR.

 

%let YR=2014;
%let input=%sysfunc(ifc(&yr=2014,final_&yr,w48_cumulated_&yr));

Then your data step is the same and the macro variables supply the differences.

data SKBusiness&yr;
  set MU&yr..&input 
    (keep= clt_ID snm gvn_nm bus_inc_gross bus_inc_net prof_inc_gross
      prof_inc_net tax_prov prov_of_res mj_net_inc_aloc_sk
  );
  where (tax_prov = 7 OR (tax_prov = 13 and mj_net_inc_aloc_sk > 0)) 
    AND (bus_inc_net ne 0 
      or bus_inc_gross ne 0
      or prof_inc_net ne 0
      or prof_inc_gross ne 0
        )
  ;
  format prov_of_res prov_of_resfmt.
    tax_prov tax_provfmt.
  ;
run;

View solution in original post

4 REPLIES 4
Kurt_Bremser
Super User
%macro do_my_data_steps;
%if &yr = 2014 %then %do;
/* insert first data step here */
%end;
%if &yr = 2015 %then %do;
/* insert second data step here */
%end;
%mend;
%do_my_data_steps
Astounding
PROC Star

Because so much of your code is identical regardless of the year, a well-written macro would make that apparent.  For example:

 

%macro business (yr=);

 

data SKBusiness&yr;

set

 

   %if &yr=2014 %then MU2014.final_2014;

   %else %if &yr=2014 %then MU2015.w48_cumulated_2015;

 

(keep= clt_ID snm gvn_nm bus_inc_gross bus_inc_net prof_inc_gross prof_inc_net tax_prov prov_of_res mj_net_inc_aloc_sk);

where (tax_prov = 7 OR (tax_prov = 13 and mj_net_inc_aloc_sk > 0)) AND

(bus_inc_net ne 0 or

bus_inc_gross ne 0 or

prof_inc_net ne 0 or

prof_inc_gross ne 0)

;

format prov_of_res prov_of_resfmt.

tax_prov tax_provfmt.;

run;

 

%mend business;

 

Then call the macro with either year:

 

%business (yr=2014)

%business (yr=2015)

 

 

Tom
Super User Tom
Super User

The differences seem simply enough that you might not need to write a macro.  Most of the differences you can use the value of the YR macro variable to replace. The other main difference looks to be that the name of the input data set is different.  You could use the IFC() function to select between two choices based on the value of YR.

 

%let YR=2014;
%let input=%sysfunc(ifc(&yr=2014,final_&yr,w48_cumulated_&yr));

Then your data step is the same and the macro variables supply the differences.

data SKBusiness&yr;
  set MU&yr..&input 
    (keep= clt_ID snm gvn_nm bus_inc_gross bus_inc_net prof_inc_gross
      prof_inc_net tax_prov prov_of_res mj_net_inc_aloc_sk
  );
  where (tax_prov = 7 OR (tax_prov = 13 and mj_net_inc_aloc_sk > 0)) 
    AND (bus_inc_net ne 0 
      or bus_inc_gross ne 0
      or prof_inc_net ne 0
      or prof_inc_gross ne 0
        )
  ;
  format prov_of_res prov_of_resfmt.
    tax_prov tax_provfmt.
  ;
run;
mkeintz
PROC Star

You don't need two data steps, since they are identical, with the exception of the output dataset names and the input dataset names.  Make a single datastep with macrovars, changing only the DATA statement and the SET statement:

 

data SKBusiness&year;

  set MU&year..%sysfunc(ifc(&year=2014,final_&year,w48_cumulated_&year))

 

This means you only have to maintain one data step, assuming you want to do the same processing to both the 2014 and 2015 data.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 5643 views
  • 9 likes
  • 5 in conversation