Trying to create a list of datasets that exist, so I can use that list in a data step to create a combined dataset.
Below code is the best I can do. Can someone show me how to do this ?
%macro setvars;
%global mylist = '';
%if %sysfunc(exist(MYRAW.&mysid.MBS1)) %then
mylist = cats(mylist,VARS_MBS1);
%if %sysfunc(exist(MYRAW.&mysid.DWS1)) %then
mylist = cats(mylist,VARS_DWS1);
%if %sysfunc(exist(MYRAW.&mysid.FMS1)) %then
mylist = cats(mylist,VARS_FMS1);
%if %sysfunc(exist(MYRAW.&mysid.PSS1)) %then
mylist = cats(mylist,VARS_PSS1);
%mend setvars;
%setvars;
data work.all_vars_1;
set &mylist.;
run;
Hi,
If you want to concatenate text in a macro you can use a %let statement to assign the new value and then place the new text you want next to the existing text.
E.g., in place of:
mylist = cats(mylist,VARS_PSS1);
you can use:
%let mylist = &mylist VARS_PSS1;
and then similarly for the other assignments.
If this does not work then please also share the log using the "</>" icon, including the code and any messages - especially errors.
Thanks & kind regards,
Amir.
Hi,
If you want to concatenate text in a macro you can use a %let statement to assign the new value and then place the new text you want next to the existing text.
E.g., in place of:
mylist = cats(mylist,VARS_PSS1);
you can use:
%let mylist = &mylist VARS_PSS1;
and then similarly for the other assignments.
If this does not work then please also share the log using the "</>" icon, including the code and any messages - especially errors.
Thanks & kind regards,
Amir.
It looks (from the code) that you are looking 4 particular data sets in one library, why not to use the power of dictionary.tables?
proc sql;
select catx(".",libname,libname)
into :myList separated by " "
from dictionary.tables
where libname='MYRAW'
and memname in (
"&mysid.MBS1"
,"&mysid.DWS1"
,"&mysid.FMS1"
,"&mysid.PSS1"
)
;
quit;
data want;
set &myList.;
run;
Bart
To concatenate text strings in macro code just write the text string next to each other.
For example to concatenate B to A just type AB. So to append dataset5 to the value of mylist just type it next to the value of mylist. Like this: &mylist dataset5
%macro setvars;
%if not %symexist(mylist) %then %global mylist;
%let mylist=;
%if %sysfunc(exist(MYRAW.&mysid.MBS1)) %then %let mylist = &mylist MYRAW.&mysid.MBS1;
%if %sysfunc(exist(MYRAW.&mysid.DWS1)) %then %let mylist = &mylist MYRAW.&mysid.DWS1;
%if %sysfunc(exist(MYRAW.&mysid.FMS1)) %then %let mylist = &mylist MYRAW.&mysid.FMS1;
%if %sysfunc(exist(MYRAW.&mysid.PSS1)) %then %let mylist = &mylist MYRAW.&mysid.PSS1;
%mend setvars;
For you actual problem perhaps you want the macro to just emit the names directly instead of building a macro variable.
%macro datsets(list);
%local i ds ;
%do i=1 %to %sysfunc(countw(&list,%str( )));
%let ds=%scan(&list,&i,%str( ));
%if %sysfunc(exist(&ds)) %then &ds ;
%end;
%mend datasets;
Then you could use it like this:
data work.all_vars_1;
set %datasets(MYRAW.&mysid.MBS1 MYRAW.&mysid.DWS1 MYRAW.&mysid.FMS1 MYRAW.&mysid.PSS1);
run;
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.