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

Hello

What is the way to write this code with proc append?

 

 Data ttt2101;
input id profit mon;
cards;
1 982 200 2101
2 978 400 2101
;
Run;

Data ttt2102;
input id profit mon;
cards;
1 982 250 2102
2 978 490 2102
;
Run;

Data ttt2105;
input id profit mon;
cards;
1 982 150 2105
2 978 300 2105
;
Run;

%let month_List=2101+2102+2105;
%let n=3;

%macro sset; 
%do j=1 %to &n.;
%let mon=%scan(&month_List.,&j.,+);
ttt&mon.
%end;
%mend sset;
%put %sset;

data ALL_tbl;
SET  %sset;
Run;
 
1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Here's one way:

%let month_List=2101+2102+2105;
%let n=3;

%macro sset; 
%do j=1 %to &n.;
%let mon=%scan(&month_List.,&j.,+);
proc append data=ttt&mon.
 base=all_tbl;
run;
%end;
%mend sset;
%sset

This assumes that the data set all_tbl does NOT already exist.

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

So, first, I want to say that no macros are needed here.

 

data all_tbl;
    set sset:;
run;

But since PROC APPEND is a better choice for when you have large data sets, you can do essentially the same macro looping as you are doing, and call PROC APPEND n times, where in this case n=3 because you have 3 data sets. As always, you should write the code without macros first and get that to work, and then the actual macro loop ought to be obvious. (Show us that code without macros if you are having trouble).

 

I recommend you go ahead and give writing such a macro a try, as it doesn't really seem to be difficult, or even that different, given your current looping. Show us what you have tried.

--
Paige Miller
Ronein
Meteorite | Level 14
Sure but this method is a bit risky.
Need to be sure that there are no other datasets with same start...
Astounding
PROC Star

Here's one way:

%let month_List=2101+2102+2105;
%let n=3;

%macro sset; 
%do j=1 %to &n.;
%let mon=%scan(&month_List.,&j.,+);
proc append data=ttt&mon.
 base=all_tbl;
run;
%end;
%mend sset;
%sset

This assumes that the data set all_tbl does NOT already exist.

Ksharp
Super User
%let month_List=2101+2102+2105;
%let sset=ttt%sysfunc(prxchange(s/\+/ ttt/,-1, &month_List. ))  ;

%put &=sset. ;

data ALL_tbl;
SET  &sset. ;
Run;

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!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 642 views
  • 2 likes
  • 4 in conversation