DATA Step, Macro, Functions and more

Simple Macro Discussion

Reply
Contributor
Posts: 67

Simple Macro Discussion

Hello!

I have a simple macro question; if anyone has better sources to read up on macros, I would also appreciate it (I've been reading up a bit on google, but can't seem to find my error here)

I have two datasets called: data.neds09merge1 and data.neds10merge1

I want to add a variable for each year (2009, 2010) for the corresponding data sets above. So Data.neds09merge1 will have 2009 for everyone in the variable "year," and data.neds10merge1 will have 2010 for everyone in the variable "year."

Then, I want "yearz" to be 09 or 10, corresponding to the 2009 and 2010.

So basically I'm trying to do this but make a macro for it

data one;

     set data.neds09merge1;

     year = 2009;

run;

data two;

     set data.neds10merge1;

     year = 2010;

run;

but for some reason my macro is giving me trouble.

%Macro stack(set,yearz);

  data &set;

  set data.neds&&year&merge1;

  year = 20&yearz;

  run;

%mend stack;

%stack(set = one, year = 09);

%stack(set = two, year = 10);

The error I get:

WARNING: Apparent symbolic reference MERGE not resolved.

I think it has something to do with the & after "year" and preceding "merge1." I'm not 100% sure though. Thoughts?Thanks! -Gina

Trusted Advisor
Posts: 1,934

Re: Simple Macro Discussion

You don't have a macro variable &merge1

You also seem to have spelling differences, in one place the macro variable is yearz, and other places it is &year

Try

set data.neds&yearz.merge1;

Super User
Posts: 11,343

Re: Simple Macro Discussion

Unless you have someone dictating a policy to use 2 digit years in file/dataset names I would strongly recommend consider naming the original datasets as neds2009merge.

Then you don't have to calculate such things as 20+&year. Also it will make looping over datasets with macros or using call execute easier:

%do year=2008 %to 2012;

proc freq data=data.neds&year.merge1;

<other common code>

run;

%end;

for a rough example.

Super User
Posts: 7,868

Re: Simple Macro Discussion

%macro stack(set,year);

data &set;

set data.neds&year.merge1; *the dot after year terminates the macro variable name and signals to the macro interpreter that the following constitutes "plain" program text not to be treated by it;

year = 20&year;

run;

%mend;

%stack(one,09);

%stack(two,10);

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Super User
Posts: 7,997

Re: Simple Macro Discussion

I am not entirely sure what the point of the macro and all the code is.  Why not just do:

data want;

     set neds09merge1 (in=a) neds10merge1 (in=b);

     if a then year=2009;

     else year=2010;

run;

Ask a Question
Discussion stats
  • 4 replies
  • 309 views
  • 0 likes
  • 5 in conversation