DATA Step, Macro, Functions and more

Using macro to name a data file

Reply
Occasional Contributor
Posts: 15

Using macro to name a data file

Hi,

I am trying to use the following macro to name my data file but it is not working but not sure why. It gets stuck on "data temp_&yymm._seg&i". It seems to get stuck due to having 2 macro variables in the file name.

Could I get some help? Thank you.
================================
%macro name_data(yymm=,i=);

data temp_&yymm_seg&i ;

merge data1_seg&i (in=a)
data2_&yymm_seg&i (in=b);

by common_var;

run;
%mend name_data;
SAS Super FREQ
Posts: 8,743

Re: Using macro to name a data file

Actually, there is a HUGE difference between:
temp_&yymm._seg&i as a macro variable reference (with the dot[.] ) and temp_&yymm_seg&i without the dot [.] in the name.

Your code does NOT show the dot. I would suspect that you are getting messages something like this:
[pre]
WARNING: Apparent symbolic reference YYMM_SEG not resolved.
[/pre]

Or something similar if you do NOT have a single dot that shows the macro processor where one macro variable ends and the rest of the string begins. If you need to have a dot in a resolved macro variable name, such as you needed to build a dataset name from 2 macro variables, then you use 2 dots -- one dot for the delimiter and the second dot as part of the resolved value, like this:
[pre]
%let lib = perm;
%let myfile = cars;

proc print data=&lib.&myfile <---would be wrong
proc print data=&lib..&myfile <---would be correct
[/pre]

For more information about macro variable concatenation to build text strings from multiple macro variables, consult the macro documentation. In addition, this paper is a good introduction to macro processing concepts.
http://www2.sas.com/proceedings/sugi28/056-28.pdf

cynthia
Occasional Contributor
Posts: 15

Re: Using macro to name a data file

I just saw your reply and thank you for your reply, Cynthia!
Ask a Question
Discussion stats
  • 2 replies
  • 136 views
  • 0 likes
  • 2 in conversation