BookmarkSubscribeRSS Feed
Caroline1620_
Calcite | Level 5

Hello! Im wondering if there is a way to include the filename in the output for proc mean. I have a loop that does proc mean for a series of files, but in the output I only get the varname to identify each output I would also like to add the file name

 

 

 

%macro do_nftest (allanftest);

%do p=1 %to %sysfunc(countw(&allanftest));
proc means data= %scan(&allanftest,&p);
var fr11;

run;
%end;

%mend;

%do_nftest(&allanftest.);

 

Thankful for any help!

 

 

 

 

5 REPLIES 5
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,


Can you provide example of your data, and example of your output as its not clear just from the code.  If you want the dataset name in the output then a simple change of mindset from repeating code to setting your data together and by group processing it.  E.g.

I have three datasets A, B, C:

data for_test;

  set a (in=a) b (in=b) c (in=c);

  length dataset_name $8;

  if a then dataset_name="A";

  else if b then dataset_name="B";

  else dataset_name="C";

run;

proc means data=for_test;

  by dataset_name;

  var fr11;

  output out=results n=n ...;

run;

 

As you can see form the above, I combine my data, then run one proc means with a by grouping on it.  This is both easier to code, and is better performance, no need for macro code at all.

ChrisNZ
Tourmaline | Level 20
Or something like:

data _V/view=_V;
set &allanftest. indsname=_IN;
SOURCE=_IN;
run;

proc means data= _V;
by SOURCE notsorted;
var FR11;
run;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Nice, I always forget the indsname option.

PaigeMiller
Diamond | Level 26

I don't know if this meets your needs, but you could easily put the filename in the title.

 

title "Using Data Set %scan(&allanftest,&p)";

 

 

--
Paige Miller
DartRodrigo
Lapis Lazuli | Level 10

Hi Caroline,

 

You can do this by making a table list of the files in that directory, then you substr() the

var to get only the file name, create a variable as an ID that set first file as 1 and so on.

The syntax would be similar to this:

 

filename file "ls /path/*.txt";

data test;
   length entire_row $ 160;
   infile file;
   input entire_row;
run;

data test2;
   set test;
   id = _N_;
   new_row = substr(entire_row,1,1);
run;
 
/*Here create a macro with %do loop to recognize the id*/

proc sql noprint;
   select new_row into: nfile&i. from test2
   where id = &i.;
quit;

In proc sql create a macro step to create multiple macros containing file names.

 

After this use in your proc means %do loop, i would say to put in the same

%do loop to do this.

 

Hope this helps

 

Att

 

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
  • 5 replies
  • 1109 views
  • 4 likes
  • 5 in conversation