BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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.
2 REPLIES 2
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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
http://support.sas.com/documentation/cdl/en/lrdict/61724/HTML/default/a000245924.htm


Scott Barry
SBBWorks, Inc.
deleted_user
Not applicable
Hello youngsh,

Here is a little example on how to do it:

Let's simulate some data to illustrate the program:

data T01_20091001
T01_20091002
T01_20091003;
do i=1 to 10000;
x=ranuni(0);
select;
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 !";
end;
end;
run;

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

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;
quit;

proc append data=T03_temp out=T04_union;
run;

proc sql;
drop view T03_temp;
quit;
%mend;

Let us invoke the macro "manually"
* import 1 file manually;
%import_file_with_date(20091001);

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,"_"),');'));
run;

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

I hope this helps?

yoba Message was edited by: yoba

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 2335 views
  • 0 likes
  • 2 in conversation