I have multiple textfiles seperated by length that I want to import. I want to know which textfile that is the source as a variable in the imported data. I import data with the following statement (it's simplified, there's 20+ variables in the original).
DATA WORK.DATA;
LENGTH
Debitor $ 10
Udligning 8
;
FORMAT
Debitor $CHAR10.
Udligning DATE9.
;
INFORMAT
Debitor $CHAR10.
Udligning DATE9.
;
INFILE '/home/…/*.TXT'
LRECL=32767
FIRSTOBS=3
ENCODING="LATIN9"
DLM='7c'x
MISSOVER
DSD ;
INPUT
Debitor : $CHAR10.
Udligning : ?? ANYDTDTE9.
;
RUN;
If I have two sources in my infil directory - source1.txt and source2.txt I want my data too look like this:
Debitor Udligning Source
ABCD 10JAN2019 Source1
EFGH 15JAN2019 Source1
IJKL 10FEB2019 Source2
Where the first two rows are from source1.txt and the second from source2.txt. Can anyone help me how to retrieve the txt filename and add it as a variable.
Thanks.
Show the code you are using to read the data.
Likely you want the infile option Filename and an assignment to keep it in the data:
data read; infile fileref filename=infilename; length readfile $50.; readfile = infilename; run;
make sure the length of the readfile variable will hold the path plus the name of the data file.
The variable Infilename would be a temporary variable not written to the data set.
If you are reading series of files in a single data step the Filename variable value will change to reflect the current source.
Show the code you are using to read the data.
Likely you want the infile option Filename and an assignment to keep it in the data:
data read; infile fileref filename=infilename; length readfile $50.; readfile = infilename; run;
make sure the length of the readfile variable will hold the path plus the name of the data file.
The variable Infilename would be a temporary variable not written to the data set.
If you are reading series of files in a single data step the Filename variable value will change to reflect the current source.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.