DATA Step, Macro, Functions and more

capture dataset name as a variable in a new dataset

Posts: 0

capture dataset name as a variable in a new dataset

I am building a dataset from several large date dependent datasets created by another user. The dataset name has the date value in it. I want to capture the datevalue from the users dataset name and add it to a new variable on my datset.

For example if dataset a contained the name testresults01/01/2009, I will take all the data elements from testresults01/01/2009 and add a variable which contains the value 01/01/2009.

Hope that makes sense.
Super Contributor
Super Contributor
Posts: 3,174

Re: capture dataset name as a variable in a new dataset

Explore using the "pathname" function and consider that some characters are not acceptable in a SAS variable name. So, you will likely already have a "libref" setup before your DATA step, so you will need to consider how to pre-process the info returned from the pathname function, either in a different DATA step or by using MACRO language and %SYSFUNC(pathname()) to assign a macro variable. Then this macro variable would be used later in your input processing DATA step to read the actual file and populate your variables.

SAS 9.2 Language Dictionary: PATHNAME Function

Scott Barry
SBBWorks, Inc.
Posts: 0

Re: capture dataset name as a variable in a new dataset

Hello youngsh,

Here is a little example on how to do it:

Let's simulate some data to illustrate the program:

data T01_20091001
do i=1 to 10000;
when(mod(i,3)=0) output T01_20091001;
when(mod(i,3)=1) output T01_20091002;
when(mod(i,3)=2) output T01_20091003;
otherwise put "Never seen !";

data T02_metadata;
to_import="T01_20091001"; output;
to_import="T01_20091002"; output;

This macro will append the content of the file T01_date to T04_union. The date is added to the input file before the append takes place. This is done with a "view" (this doesn't require disk space)

%macro import_file_with_date(date);
proc sql;
create view T03_temp as
select input("&date",yymmdd8.) format=date9. as date, *
from T01_&date;

proc append data=T03_temp out=T04_union;

proc sql;
drop view T03_temp;

Let us invoke the macro "manually"
* import 1 file manually;

You can generate macro calls using a data step
* import multiple files from list in T02_metadata;
data _NULL_;
set T02_metadata;
call execute(cats('%import_File_with_date(',scan(to_import,2,"_"),');'));

You could add some code in the macro to handle re-loads.

I hope this helps?

yoba Message was edited by: yoba
Ask a Question
Discussion stats
  • 2 replies
  • 2 in conversation