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

Hello everybody

I usualy use Guide to work but to be able to go a litte furter on my work I'm doing some steps in SAS Base (inside Guide). I've been looking for a solution that permits to append a variable number of tables - like in a loop/do while/etc. cicle. What I have today is a manual step by step process:

 

 

data STUDY;

set

     STUDY_0

     STUDY_1

     STUDY_2

     STUDY_3

     STUDY_4

   (AND SO ON...); 


run;

 

 

 

If I need to add more tables I have to do it maualy.

 

Does anyone know a way to go arround this? I thought in someting like:

 

 

%macro doappendtable(a=);

%do d=0 %to &a.;

 STUDY_&d.;

%end;

run;

%mend;

 

 

 

%doappnedtable(a=4);

 

 

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

If you want ALL of the data set names that start with STUDY_ you could use:

 

set study_: ;

View solution in original post

5 REPLIES 5
Astounding
PROC Star

If you want ALL of the data set names that start with STUDY_ you could use:

 

set study_: ;

AnaC
Fluorite | Level 6

Thanks!

It woks perfectly!!

PaigeMiller
Diamond | Level 26
%macro doappendtable(a=);
%do d=0 %to &a.;
 STUDY_&d.     
%end;
%mend;

data study;
    set %doappendtable(a=4);
run;

Don't forget the semi-colon after calling the macro. And no semi-colon as in your original code after STUDY_&D

--
Paige Miller
AnaC
Fluorite | Level 6

The semi.colon issue is very importante. Otherwise I was almost there 🙂

Thnaks once more guys.

PaigeMiller
Diamond | Level 26

So here's something to think about when or if you try writing another macro.

 

Macros are simply used to substitute text into your SAS program. Where you actually see macro variables or macro code in your program, the text values created by the macro code are substituted into SAS when you execute your program.

 

And when this substitution happens, it must result in valid legal SAS code with no errors, and obviously, the code must do what you want. That's so important, I'm going to say it again.

 

when this substitution happens, it must result in valid legal SAS code with no errors

 

So if your original code has

 

 STUDY_&d.;

then when &d is 1, the text placed into the code when SAS executes is

 

STUDY_1;

 

and the semi-colon is wrong if you are expecting to add additional data set names to the SET statement. Why? Because the result would be

 

SET STUDY_1; STUDY_2; STUDY_3;

 

and this will produce errors.

--
Paige Miller

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 5 replies
  • 1407 views
  • 0 likes
  • 3 in conversation