BookmarkSubscribeRSS Feed
yashij2138
Calcite | Level 5

I want to make a union of all datasets from 2010-2019 whcih are named like students_201001, students_201002....students_201012.....students_201905.

 

I was using a nested loop one for year and other for moth, but it is not working:

 

%macro join;
%Do i=2010 %to 2019;
%DO j=01 %TO 12;
data append_recov_&i&j;
set chi_perf.perf_chi_&i
run;
%end;
%end;
%mend;
%join;

 

Please help!

4 REPLIES 4
Tom
Super User Tom
Super User

The iterative %DO loop will convert your values into numbers and then back to strings to assign to the loop variable.

So %DO j=01 %to 12 will set J to 1,2,3,4,5,6,7,8,9,10,11,12.

If you want to convert the values less than 10 to 2 digits try using the PUTN() function.

%do j=01 %to 12;
  %let j=%sysfunc(putn(&j,z2.));

 

Astounding
PROC Star

An easy way to get YYYYMM is to create a third macro variable:

 

%do y=2010 %to 2019;
   %do m = 1 %to 12;
      %let ym = %eval(&m + 100 * &y);
      %put The year and month:  &ym;
   %end;
%end;
Ksharp
Super User
data want;
set students_201001 - students_201910 ;
run;

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 4 replies
  • 995 views
  • 2 likes
  • 5 in conversation