BookmarkSubscribeRSS Feed
ginak
Quartz | Level 8

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

4 REPLIES 4
PaigeMiller
Diamond | Level 26

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;

--
Paige Miller
ballardw
Super User

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.

Kurt_Bremser
Super User

%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);

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 842 views
  • 0 likes
  • 5 in conversation