BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
qiali
Calcite | Level 5
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

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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;

View solution in original post

4 REPLIES 4
Astounding
PROC Star

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
Calcite | Level 5
Thank you so much!!!!!!
Reeza
Super User

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

morgalr
Obsidian | Level 7
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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 1042 views
  • 5 likes
  • 4 in conversation