Hello, i have 12 datasets that have data for each month of the year and I'd like to search for the data for 3 people within all data and produce a stacked dataset of all data at the end containing all data for these people in the year.
The data sets are labeled as such: Data201910 , Data201911, Data201912, et. The last two digits represent the month
I need to pull data for person 1, 2, and 3.
How would I make an array to do this quickly rather than querying each data set?
Hi @jmmedina25
According to your description, array seems not to be the right solution, as array can loop through multiple variables but not datasets.
I believe you nee a macro to make this repetitive task.
I have written the below code. Please adapt the WHERE statement and test it.
Thank you
/*UNTESTED CODE*/
%macro _search;
%do i=1 %to 9;
data want&i;
set Data20190&i;
where ID in (1,2,3); /*to be adapted according to your variables*/
run;
%end;
%do i=10 %to 12;
data want&i;
set Data2019&i;
where ID in (1,2,3); /*to be adapted according to your variables*/
run;
%end;
%mend;
%_search;
data want;
set want:;
run;
Hi @jmmedina25
According to your description, array seems not to be the right solution, as array can loop through multiple variables but not datasets.
I believe you nee a macro to make this repetitive task.
I have written the below code. Please adapt the WHERE statement and test it.
Thank you
/*UNTESTED CODE*/
%macro _search;
%do i=1 %to 9;
data want&i;
set Data20190&i;
where ID in (1,2,3); /*to be adapted according to your variables*/
run;
%end;
%do i=10 %to 12;
data want&i;
set Data2019&i;
where ID in (1,2,3); /*to be adapted according to your variables*/
run;
%end;
%mend;
%_search;
data want;
set want:;
run;
Hi @jmmedina25 Since your datasets have an easy naming sequence, all you probably need is
data want;
set Data201901-Data201912;
where person in (1,2,3);
run;
This worked well and is simple, thank you!
You're welcome ![]()
April 27 – 30 | Gaylord Texan | Grapevine, Texas
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!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.