2
votes

So using SAS, I have a number of SAS monthend datasets named as follows:

mydata_201501
mydata_201602
mydata_201603
mydata_201604
mydata_201605
...
mydata_201612

Each has account information at particular monthend. I want to stack the datasets all into one dataset using colon rather than writing out the full set statement as follows:

data mynewdata;
set mydata_:;
run;

However there is no datestamp variable within the datasets so when I stack them I will lose the monthend information for each account. I want to know which line refers to which monthend for each account. Is there a way I can automatically create a variable that names the table the row come from. for example the long winded way would be this:

data mynewdata;
set mydata_201501 (in=a) mydata_201502 (in=b) mydata_201503 (in=c)...;
if a then tablename = 'mydata_201501';
if b then tablename = 'mydata_201502';
if c...
run;

but is there a quicker way using colon along these lines?

data mynewdata;
set mydata_:;
tablename = _tablelabel_;
run;

thanks

2
It would appear this is what you're looking for: support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/…mjsqu

2 Answers

2
votes

I always find clicking on comment links annoying, so hopefully here's the answer in your context. Use the INDSNAME= SET statement option to assign the dataset name to a variable:

data mynewdata;
  set mydata_: indsname=_tablelabel_;
  tablename = _tablelabel_;
run;

N.B. you can call _tablelabel_ whatever you want, and you may wish to change it so it doesn't look like a SAS generated variable name.

INDSNAME= only became a SAS SET statement option in version 9.2

1
votes

Just to be clear, with my particular code, where the datasets were named mydata_yyyymm and I wanted a monthend variable with datestamp, I was able to produce this using the solution provided by mjsqu as follows (obs and keep statement provided if required):

data mynewdata;
  set mydata_: (obs=100 keep=xxx xxx) indsname=_tablelabel_;
  format monthend yymmdd10.;
  monthend = input(scan(_tablelabel_,-1,'_'),yymmn6.);
run;