BookmarkSubscribeRSS Feed
Robin_moon
Fluorite | Level 6

I saw a loop code the other day. And it generate files of output_&i (i.e. output_1, output_2,..., output_n). At the end, it row bind all the files as:

data all_results;
     set output_:;
run;

It seems that we could list files output_1, output_2, ... , output_n in a simple way by using ":" as output_:, so I tried to row bind my files that all end up with "_output" (i.e. 1_output, 2_output, 3_output) in a similar way:

 

data all_results;
    set :_output;
run;

But it failed. I'd like to know how to write the code in the right way. Thanks!

4 REPLIES 4
Kurt_Bremser
Super User

The : wildcard works only for variable parts at the end of a dataset name.

proc sql noprint;
select memname into :datasets separated by " "
from dictionary.tables
where libname = "WORK" and memname like '%_OUTPUT';
quit;

data all_results;
set &datasets.;
run;
Tom
Super User Tom
Super User

The : wildcard only works at the end.

 

The simplest solution is to change your naming pattern to use a common prefix instead.

 

Otherwise you will have to build your own logic to make the list.  If the list is small enough (less than 64K bytes) then you can insert it into a macro variable.

proc sql noprint;
%let dslist=;
select nliteral(memname) into :dslist separated by ' '
from dictionary.members
where libname='WORK'
  and memtype in ('DATA','VIEW')
  and memname like '%COUNT'
;
quit;

data countall;
  set &dslist;
run;

 

Note that if you are using the numeric suffix then you can a range of values instead.  That way you could reference COUNT1 to COUNT5 and also ignore COUNTALL.

data countall;
  set count1-count5;
run;

 

Reeza
Super User

Here is a reference that illustrates how to refer to variables and datasets in a short cut list:
https://blogs.sas.com/content/iml/2018/05/29/6-easy-ways-to-specify-a-list-of-variables-in-sas.html

 

The method you're looking for unfortunately doesn't easily exist. The are workarounds (see @Tom 's answer) and other methods you can use are in the post above.

 


@Robin_moon wrote:

I saw a loop code the other day. And it generate files of output_&i (i.e. output_1, output_2,..., output_n). At the end, it row bind all the files as:

data all_results;
     set output_:;
run;

It seems that we could list files output_1, output_2, ... , output_n in a simple way by using ":" as output_:, so I tried to row bind my files that all end up with "_output" (i.e. 1_output, 2_output, 3_output) in a similar way:

 

data all_results;
    set :_output;
run;

But it failed. I'd like to know how to write the code in the right way. Thanks!


 

Robin_moon
Fluorite | Level 6
Thanks for sharing the link. So sad to hear from that. SAS has a shortcut for selecting variables with same prefix but not for suffix.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 664 views
  • 1 like
  • 4 in conversation