BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
simmwa
Fluorite | Level 6

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;
1 ACCEPTED SOLUTION

Accepted Solutions
Amir
PROC Star

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.

View solution in original post

4 REPLIES 4
Amir
PROC Star

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.

simmwa
Fluorite | Level 6
Thanks ! Works perfect now. Its annoying when the solution is so simple 🙂
yabwon
Amethyst | Level 16

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Tom
Super User Tom
Super User

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;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 552 views
  • 3 likes
  • 4 in conversation