DATA Step, Macro, Functions and more

including filename in proc means output

Reply
New Contributor
Posts: 4

including filename in proc means output

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!

 

 

 

 

Super User
Super User
Posts: 7,942

Re: including filename in proc means output

Posted in reply to Caroline1620_

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.

PROC Star
Posts: 1,759

Re: including filename in proc means output

Posted in reply to Caroline1620_
Or something like:

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

proc means data= _V;
by SOURCE notsorted;
var FR11;
run;
Super User
Super User
Posts: 7,942

Re: including filename in proc means output

Nice, I always forget the indsname option.

Trusted Advisor
Posts: 1,913

Re: including filename in proc means output

Posted in reply to Caroline1620_

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)";

 

 

Regular Contributor
Posts: 212

Re: including filename in proc means output

[ Edited ]
Posted in reply to Caroline1620_

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

 

Ask a Question
Discussion stats
  • 5 replies
  • 247 views
  • 4 likes
  • 5 in conversation