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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 253 views
  • 1 like
  • 4 in conversation