BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
chenyiwen1717
Fluorite | Level 6

Hi All,

I have mutitlple SAS data sets with names file91 to file14. The last two digits represents years (e.g.91 represents 1991 and 00 represents 2000). I tried to run a macro to generate some results by reading these names. However, I failed to concanate file names within the macro. Could you one help? Thanks.

%macro getall;

	%do i=91 %to 99;  /*year 1991 to 1999*/
	%getpart1(file&i);
	%end;

	%do i=0 %to 14;  /*year 2000 to 2014*/
	%getpart2(%sysfunc(cats('file',put(&i,z2.))));
	%end;

%mend getall;
%getall

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Assuming that somewhere you have properly defined %GETPART1 and %GETPART2, your top loop should be fine  (Of course I'm not yet convinced that you have defined those other macros, but you'll be able to comment about that easily enough.)

 

The bottom loop requires a few changes, though.  CATS is not needed to concatenate strings in macro language, nor are quotes needed to define a character string.  And %SYSFUNC cannot be applied to PUT (only to PUTN or PUTC).  So you would need:

 

%do i=0 %to 14;

%getpart2 (file%sysfunc(putn(&i, z2)))

%end;

 

View solution in original post

4 REPLIES 4
Astounding
PROC Star

Assuming that somewhere you have properly defined %GETPART1 and %GETPART2, your top loop should be fine  (Of course I'm not yet convinced that you have defined those other macros, but you'll be able to comment about that easily enough.)

 

The bottom loop requires a few changes, though.  CATS is not needed to concatenate strings in macro language, nor are quotes needed to define a character string.  And %SYSFUNC cannot be applied to PUT (only to PUTN or PUTC).  So you would need:

 

%do i=0 %to 14;

%getpart2 (file%sysfunc(putn(&i, z2)))

%end;

 

chenyiwen1717
Fluorite | Level 6

Thanks Astounding. This is exactly what I want! I shouldn't put the concatenate function or quotes to concatenate string in macro! This sloved my question!Smiley Wink

Reeza
Super User

You can also use a single loop:

 

%macro getall;
    %do year=1991 %to 2014;
        %put file%substr(&year, 3, 2);
    %end;
%mend getall;

%getall;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Firstly, I would always suggest to combine all "like" data together in one dataset - with a date column for the year if need be.  This will considerably shrink your programming effort and make your life much easier.

You can of course loop by using:

data _null_;
  set sashelp.vtable (where=(libname="WORK" and substr(memname,1,4)="FILE"));
  /* This bit gets generated for each filexx dataset */
  call execute('%getpart1 ('||strip(memname)||');');
  call execute('%getpart2 ('||strip(memname)||');');
run;

You don't need any hardcoding or loops then of the files, as anything in work with the prefix file will be processed.

I would still recommend combining them though.

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
  • 1127 views
  • 2 likes
  • 4 in conversation