BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Theis
Calcite | Level 5

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.

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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.

View solution in original post

2 REPLIES 2
ballardw
Super User

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.

Theis
Calcite | Level 5
Thanks 🙂

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 417 views
  • 4 likes
  • 2 in conversation