BookmarkSubscribeRSS Feed
dht115
Calcite | Level 5

Hello, 

 

I am running a SAS macro to sort my dataset. I do have multiple folders and each folder has different number of datasets. Is there a way for us to modify SAS macro which can go to specific folder and bring all sas file name as macro parameter. So I just need to change library name and run the macro instead of changing sas dataset name for macro variable. 

 

  • For scenario 1, I do have five (5) sas files in folder so I need to run my macro five times. 
  • Whereas, in scenario 2, I do have three (3) sas files in folder - and I do need to run my macro only three times. 

Scenario 1: 

dht115_0-1682628118181.pngdht115_2-1682628299593.png

 

Scenario 2: 

dht115_1-1682628153333.png      dht115_3-1682628315963.png

 

 

 

libname abc 'C:\Users\XXX\Desktop\platform\one'; 
%macro test (var);
proc sort data=&var. out=new_sort_&var.;
by item;
run;
%mend;

%test(test1);
%test(test2);
%test(test3);
%test(test4);
%test(test5);

 

 

6 REPLIES 6
A_Kh
Lapis Lazuli | Level 10

Use DICTIONARY.TABLES to import all dataset names from the directory, then load them into a macro variable. In the macro definition use loop to sort all datasets dynamically without physically referring to a dataset name. 

Eg: 

%macro sort(dir=);
	proc sql noprint;
		select distinct memname
			into: list separated by ' ' 
				from dictionary.tables
			where libname eq "%upcase(&dir)";
		%put &=list;
	quit;
	%do i=1 %to %sysfunc(countw(&list));
		%let var= %scan(&list, &i);
		proc sort data=&var. out=new_sort_&var.;
			by item;
		run;
	%end;
%mend;

%sort(dir= abc);
dht115
Calcite | Level 5

Getting an error: 

ERROR: File WORK.LIST.DATA does not exist.

PaigeMiller
Diamond | Level 26

Whenever you get an error in the log, show us the ENTIRE log for this code. Do not show us partial logs.

 

Since this involves macros, please turn on the macro debugging option by running the code below and then run your macro again and show us the ENTIRE log. Do not show us partial logs.

 

options mprint;
--
Paige Miller
Quentin
Super User

@dht115 wrote:

Getting an error: 

ERROR: File WORK.LIST.DATA does not exist.


The most likely way that could happen would be if you forgot an & in:

%let var= %scan(&list, &i);  %* <-- check there is an & in front of list ;

Another possibility is that  you didn't create the libref ABC prior to running the macro.  In that case you will see in the log that the select statement returns 0 obs and it does not create the macro variable LIST.  So you would have unresolved macro variable reference message in the log as well.

The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.
A_Kh
Lapis Lazuli | Level 10

Log says: "WORK.LIST data does not exist".  Macro will read data from ABC library. You need to declare LIBNAME first, then use this lib as macro parameter. 

Libname abc "C/myfolder/myfiles";

yabwon
Onyx | Level 15

Try like this:

 

Test data:

options dlcreatedir;
libname t "R:\test1";

data t.a t.b t.c t.d t.e;
  set sashelp.class;
run;

libname t "R:\test2";

data t.a t.b t.c t.d t.e;
  set sashelp.class;
run;

libname t "R:\test3";

data t.a t.b t.c t.d t.e;
  set sashelp.class;
run;

Sorting for multiple directories:

%macro sort(lib,by);
  libname t "&lib.";
  proc contents data=t._ALL_ out=list noprint;
  run;
  proc sort data=list(keep=libname memname) nodupkeys;
  by libname memname;
  run;
 
  data _null_;
    set list;
    call execute(cats("proc sort data=",libname,".",memname,"; by &by.; run;"));
  run;
  libname t clear;
%mend sort;



data _null_;
input;
call execute('%sort(' !! _INFILE_ !! ',age)'); /* <----------- you can modify sorting variables here */
cards4;
R:\test1
R:\test2
R:\test3
;;;;
run;

different directories are fed through the CARDS4 to a data step which call macro execution for datasets in that directories.

 

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



SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1697 views
  • 3 likes
  • 5 in conversation