DATA Step, Macro, Functions and more

storing imported filename in new dataset

Reply
KKJ
N/A
Posts: 1

storing imported filename in new dataset

Hello!! I´ve made this macro to import several txt files to a dataset. What i also would like to do is to save each imported txt files name into a new dataset? I´ve got no clue how i would go about this. can someone push me in the right direction? Thanks

%macro files();
%if %sysfunc(exist(saslib.data)) %then %do;
%put blabla;
%end;
%else %do;
filename infil("&path.Textfile??????.txt");
data saslib.data;
length var1 8. var2 2. var3 2.;
infile infil dlm=':' firstobs = 2;
input var1 var2 var3;
run;
%end;
%mend files;
Super Contributor
Super Contributor
Posts: 3,174

Re: storing imported filename in new dataset

You can use a RETAIN statement or a SAS assignment statement to create some SAS DATA step variable with your file name. Then on subsequent executions (not sure what you intend here), you can concatenate your permanent-named file from some LIBNAME location and add new observations as you process new input files. You will likely use the SAS EXIST function to test whether the permanent file you use for tracking already exists. Also, you can use a SAS assignment statement and a macro variable to create a new observation -- use an ATTRIB or LENGTH to establish a SAS CHARACTER variable, such as:

FileName = "&path\&whatever_macro_var_you_use";

Depending on the level of importance with this data, consider making a backup copy of the tracking file in case you have to do a restore/recovery. And you may want to add a SAS DATETIME variable (using an ATTRIB with an appropriate FORMAT) to go along with the FileName variable, such as:

LoadDate = "&SYSDATE9:&SYSTIME"DT;

Scott Barry
SBBWorks, Inc.
Ask a Question
Discussion stats
  • 1 reply
  • 117 views
  • 0 likes
  • 2 in conversation