turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- How to simplify the code in data step by macro and...

Topic Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

03-07-2018 07:19 PM

```
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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to qiali

03-07-2018 08:09 PM

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;

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Astounding

03-07-2018 08:12 PM

Thank you so much!!!!!!

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to qiali

03-08-2018 11:30 AM

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to qiali

04-02-2018 07:19 PM

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;