DATA Step, Macro, Functions and more

How to simplify the code in data step by macro and do loop?

Reply
New Contributor
Posts: 2

How to simplify the code in data step by macro and do loop?

data dataset_b;
set dataset_a;


if year = 1995 then cost = charge*ccr1995;
if year = 1996 then cost = charge*ccr1996;
if year = 1997 then cost = charge*ccr1997;
if year = 1998 then cost = charge*ccr1998;
if year = 1999 then cost = charge*ccr1999;
if year = 2000 then cost = charge*ccr2000;
if year = 2001 then cost = charge*ccr2001;
if year = 2002 then cost = charge*ccr2002;
if year = 2003 then cost = charge*ccr2003;
if year = 2004 then cost = charge*ccr2004;
if year = 2005 then cost = charge*ccr2005;
if year = 2006 then cost = charge*ccr2006;
if year = 2007 then cost = charge*ccr2007;
if year = 2008 then cost = charge*ccr2008;
if year = 2009 then cost = charge*ccr2009;
if year = 2010 then cost = charge*ccr2010;
if year = 2011 then cost = charge*ccr2011;
if year = 2012 then cost = charge*ccr2012;
if year = 2013 then cost = charge*ccr2013;
if year = 2014 then cost = charge*ccr2014;

run;

Hi All,

 

I think it is a very basic and easy question about macro and do loop.  Could anyone help to simplify it?

 

I have tried some different ways to code. None of them works....

 

Thanks a lot!

 

Qian

Super User
Posts: 6,543

Re: How to simplify the code in data step by macro and do loop?

This would be a bad application for macros for a simple reason.  The code is inefficient, when it fails to use the word ELSE.  It evaluates YEAR 20 times per observation, even when a match has already been found.  Using macro language would obscure the fact that it is inefficient code.

 

Here is another approach that still cuts down on the amount of programming, and will be a subject you should learn before worrying about macro language:

 

data dataset_b;

set dataset_a;

array ccr {1995:2014} ccr1995-ccr2014;

if (1995 <= year <= 2014) then cost = charge * ccr{year};

run;

New Contributor
Posts: 2

Re: How to simplify the code in data step by macro and do loop?

Posted in reply to Astounding
Thank you so much!!!!!!
Super User
Posts: 22,874

Re: How to simplify the code in data step by macro and do loop?

@qiali if the answer you've received solves your problem please mark it as the solution.

Contributor
Posts: 34

Re: How to simplify the code in data step by macro and do loop?

Load all of your formulas into a macro variable and quote it so the ';' is deactivated. When it is expanded into your datastep, then the formulas will all be active. We do this to load over 400 formulas from the database and have them active in our programs for calcualtions.

%let myFormulas=%str(if year = 1995 then cost = charge*ccr1995;else;
if year = 1996 then cost = charge*ccr1996;else;
if year = 1997 then cost = charge*ccr1997;else;
if year = 1998 then cost = charge*ccr1998;else;
if year = 1999 then cost = charge*ccr1999;else;
if year = 2000 then cost = charge*ccr2000;else;
if year = 2001 then cost = charge*ccr2001;else;
if year = 2002 then cost = charge*ccr2002;else;
if year = 2003 then cost = charge*ccr2003;else;
if year = 2004 then cost = charge*ccr2004;else;
if year = 2005 then cost = charge*ccr2005;else;
if year = 2006 then cost = charge*ccr2006;else;
if year = 2007 then cost = charge*ccr2007;else;
if year = 2008 then cost = charge*ccr2008;else;
if year = 2009 then cost = charge*ccr2009;else;
if year = 2010 then cost = charge*ccr2010;else;
if year = 2011 then cost = charge*ccr2011;else;
if year = 2012 then cost = charge*ccr2012;else;
if year = 2013 then cost = charge*ccr2013;else;
if year = 2014 then cost = charge*ccr2014;);
data dataset_b;
  set dataset_a;
  &myFormulas.;
run;
Ask a Question
Discussion stats
  • 4 replies
  • 246 views
  • 5 likes
  • 4 in conversation