0
votes

I've output 'Moments' from Proc Univariate to datasets. Many.

Example: Moments_001.sas7bdat through to Moments_237.sas7bdat

For the first column of each dataset (new added first column, and probably new dataset, as opposed to the original) I would like to have a particular text in every cell going down to bottom row.

The exact text would be the name of the respective dataset file: say, "Moments_001".

I do not have to 'grab' the filename, per se, if that's not possible. As I know what the names are already, I can put that text into the procedure. However, grabbing the filenames, if possible, would be easier from my standpoint.

I'd greatly appreciate any help anyone could provide to accomplish this.

Thanks, Nicholas Kormanik

3
Please mark the answer that solved your problem as accepted. If non of the answers are satisfactory explain why suggested solutions do not solve your problem. In that way SO users can post new/better solutions or improve existing answers and are also more likely to provide answers to your questions in the future.kristof

3 Answers

2
votes

Are you looking for the INDSNAME option of the SET statement? You need to define two variables because the one generated by the option is automatically dropped.

data want;
  length moment dsn $41 ;
  set Moments_001 - Moments_237 indsname=dsn ;
  moment=dsn;
run;
0
votes

I think something along these lines should be what you're after. Assuming you have a list of moments, you can loop through it and add a new variable as the first column of each dataset.

%let list_of_moments = moments_001 moments_002 ... moments_237;

%macro your_macro;

    %do i = 1 %to %sysfunc(countw(&list_of_moments.));
    %let this_moment = %scan(&list_of_moments., &i.);

        data &this_moment._v2;
            retain new_variable;
            set &this_moment.;
            new_variable = "&this_moment.";
        run;

    %end;

%mend your_macro;

%your_macro;
0
votes

The brute force entering of text into column 1 looks like this:

data moments_001;
   length text $ 16;
   set moments_001;
   text="Moments_001";
run;

You could also write a macro that would loop through all 237 data sets and insert the text.

UNTESTED CODE

%macro do_all;
  %do i=1 %to 237;
    %let num = %sysfunc(putn(&i,z3.));
    data moments_#
      length text & 16;
      set moments_#
      text="Moments_&num";
    run;
  %end;
%mend
%do_all

It seems to me (not knowing your problem) that if you use PROC UNIVARIATE with the BY option, then you wouldn't need 237 different data sets, all of your output would be in one data set and the BY variable would also be in the data set. Does that solve your problem?