09-22-2015 04:10 AM
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);
Thankful for any help!
09-22-2015 04:50 AM
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:
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";
proc means data=for_test;
output out=results n=n ...;
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.
09-22-2015 07:26 AM
10-22-2015 08:58 AM - edited 10-22-2015 09:01 AM
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