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
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;
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;
@qiali if the answer you've received solves your problem please mark it as the solution.
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;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.