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;

 

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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
  • 1091 views
  • 2 likes
  • 5 in conversation