BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Not sure if this is the right forum for this, but here goes:

I want to be able to build one data set with multiple data sets without having to write multiple set statements. The datasets are in a specific library and in the format example: hist9.aae200601

With the last 2 characters representing the month (01=January, 02=February, etc, etc).

Here is the code I have started with:

%macro names(lib,year,maxnum);
%do dt=1 %to &maxnum;
&lib&year&i;
%end;
;
%mend names;

/* Call the macro on the SET statement */

data all;
set %names(hist9.Aae,2000,5);
keep asc;
run;

So if the code runs correctly, it will build data all with data sets hist9.aae200001 - hist9.200005.

The problem is when it processes it is looking for a one digit month, so 1,2,3...instead of 01, 02, 03...so it stops without working.

Any thoughts?
3 REPLIES 3
CarstenM
Calcite | Level 5
Use the Z format for leading zeros.

&lib&year%sysfunc(putn(&dt, z2.));
deleted_user
Not applicable
Well, still can't get it to work...the Enlhist9 is a library that is automatically assigned with my AutoEx? If that helps? FYI - I changed dt to i.


84 %macro names(lib,year,maxnum);
85 %do i=1 %to &maxnum;
86 &lib&year%sysfunc(putn(&i, z2.));
87 %end;
88 %mend names;

89 data all;
90 set %names(Enlhist9.Aae,2001,5);
SYMBOLGEN: Macro variable MAXNUM resolves to 5
SYMBOLGEN: Macro variable LIB resolves to Enlhist9.Aae
SYMBOLGEN: Macro variable YEAR resolves to 2001
SYMBOLGEN: Macro variable I resolves to 1
MPRINT(NAMES): Enlhist9.Aae200101;
SYMBOLGEN: Macro variable LIB resolves to Enlhist9.Aae
SYMBOLGEN: Macro variable YEAR resolves to 2001
SYMBOLGEN: Macro variable I resolves to 2
NOTE: DATA statement used (Total process time):
real time 0.21 seconds
cpu time 0.00 seconds

ERROR: DATA STEP Component Object failure. Aborted during the COMPILATION phase.
NOTE: The SAS System stopped processing this step because of errors.
MPRINT(NAMES): Enlhist9.Aae200102;
SYMBOLGEN: Macro variable LIB resolves to Enlhist9.Aae
SYMBOLGEN: Macro variable YEAR resolves to 2001
SYMBOLGEN: Macro variable I resolves to 3
NOTE: Line generated by the macro function "SYSFUNC".
1 Enlhist9.Aae200102
------------------
557
MPRINT(NAMES): Enlhist9.Aae200103;
SYMBOLGEN: Macro variable LIB resolves to Enlhist9.Aae
SYMBOLGEN: Macro variable YEAR resolves to 2001
SYMBOLGEN: Macro variable I resolves to 4
MPRINT(NAMES): Enlhist9.Aae200104;
SYMBOLGEN: Macro variable LIB resolves to Enlhist9.Aae
SYMBOLGEN: Macro variable YEAR resolves to 2001
SYMBOLGEN: Macro variable I resolves to 5
MPRINT(NAMES): Enlhist9.Aae200105;
ERROR 557-185: Variable Enlhist9 is not an object.

91 run;
Cynthia_sas
SAS Super FREQ
Hi!
I believe your problem is the semi-colon inside your %do loop (shown below on line 86): [pre]
86 &lib&year%sysfunc(putn(&i, z2.));
[/pre] That single semi-colon will be generated for EACH filename that you are building. I think that you do NOT need to have this semi-colon here. The whole purpose of the macro is to generate a string of file names for a SET statement. There is only 1 semi-colon for a set statement. Since the macro program is essentially doing your typing for you, the best way to envision this is to work backward from a correct set statement:
[pre]
set work.file1 work.file2 work.file3 work.file4;
[/pre]
You do NOT want a semi-colon between each of the file names on the set statement. Your statement DOES have a semi-colon so what you are creating is the equivalent of this:
[pre]
set work.file1; work.file2; work.file3; work.file4;
[/pre]

So you are getting an error message on the filename for the 2nd iteration of the do loop because the libname piece of the name (up to the first dot) is not a SAS keyword and it's not a macro variable that can be resolved and so the data step stops.

To mimic what you are doing, I tested this:
[pre]
options mprint symbolgen mlogic source2;
** make some data files;
data work.FIL200601 work.FIL200602 work.FIL200603
work.FIL200604 work.FIL200605 work.FIL200606
work.FIL200607 work.FIL200608 work.FIL200609
work.FIL200610 work.FIL200611 work.FIL200612;
a=1; b=2; c=3;
run;

** define the macro program;
** note that there is NO semicolon at the end of;
** my statement that has Z. format;
%macro names(lib,year,maxnum);
%do dt=1 %to &maxnum ;
&lib&year%sysfunc(putn(&dt, z2.))

%end;
%mend names;

** use macro program;
** note semi-colon is at the END of the SET statement; ** and NOT inside the names generated by the macro ;
data all;
set %names(Work.FIL, 2006,12);
run;

** print final data set;
proc print data=all;
run;
[/pre]

And did not get any errors in the log. I can, however, replicate your error message simply by inserting a semi-colon inside the macro do loop.

Macro programming is quirky, but fun. The error messages don't always mean what you think they mean. SAS was not telling you that the libname was not assigned...it was trying to tell you that it found a text string that it didn't know how to deal with and that text string just happened to be your libname.

cynthia

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 3 replies
  • 683 views
  • 0 likes
  • 3 in conversation