How do I conditionally execute a data step

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

How do I conditionally execute a data step

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;


Accepted Solutions
Solution
‎01-24-2017 02:14 PM
Super User
Super User
Posts: 6,308

Re: How do I conditionally execute a data step

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


All Replies
Esteemed Advisor
Posts: 6,634

Re: How do I conditionally execute a data step

%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
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Respected Advisor
Posts: 4,951

Re: How do I conditionally execute a data step

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)

 

 

Solution
‎01-24-2017 02:14 PM
Super User
Super User
Posts: 6,308

Re: How do I conditionally execute a data step

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;
Super User
Posts: 787

Re: How do I conditionally execute a data step

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.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

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