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
- /
- Re: How to simplify the code in data step by macro...

Topic Options

- Subscribe to 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
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

2 weeks ago

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

Highlighted
## Re: How to simplify the code in data step by macro and do loop？

Options

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

Posted in reply to qiali

2 weeks ago

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
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Astounding

2 weeks ago

Thank you so much!!!!!!

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

Posted in reply to qiali

2 weeks ago

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