th_so
th_so

Reputation: 21

Data step multiple sets with column

I am new to SAS. Got a rather simple issue could not solve it myself.

I have 12 monthly datasets named data_1404 to data_1503, all with same set of variables. My task is to append them as the yearly dataset and add the month column to the yearly dataset.

I tried the below.

DATA data_yearly;
SET data_1404 data_1405...;
RUN;

It works however I cannot figure out how to add the month column to it. I have tried macro with &do however with no luck.

I believe this should be something very simple, will be super appreciated if someone can help me out with this. Thank you!!

Upvotes: 0

Views: 393

Answers (1)

Richard
Richard

Reputation: 27498

It sounds like you are stacking data and want one additional column that tells the downstream processes which SET the row originated from.

Use the SET option INDSNAME= to access the name of the data set from which a SET last read.

INDSNAME=variable
creates and names a variable that stores the name of the SAS data set from which the current observation is read. The stored name can be a data set name or a physical name. The physical name is the name by which the operating environment recognizes the file.

The variable is an automatic variable, such as _n_, which is added to the program data vector (PDV) but not the output data set -- you must assign an automatic variable to a 'permanent' variable for the values to be propagated to the output data set.

For example:

data x;
  length source $41;
  set
    sashelp.class
    sashelp.cars
    indsname=_source_
  ;
  source = _source_;
run;

Upvotes: 1

Related Questions