BookmarkSubscribeRSS Feed
metallon
Pyrite | Level 9

Hello SAS Experts,

 

Is there a way to read the file name acutally processed and put it in the first column of the dataset?

 

data Import_Import;
infile datalines;
length fil2read $40;
input fil2read $;
infile dummy filevar=fil2read end=done DSD
        DLM=';';
do while(not done);
INPUT
            A               : $CHAR11.
            B               : $CHAR11.
            C               : $CHAR11.
            D               : $CHAR100.
            E               : $CHAR96.
            F               : $CHAR1.
            G               : $CHAR6.
            H               : $CHAR2.
            I               : ?? COMMAX4.
            J              : $CHAR11.
            K              : $CHAR2.
            L              : ?? YYMMDD10.
            M              : $CHAR17.
            N              : $CHAR4.
            O              : $CHAR1.
            P              : $CHAR4.
            Q              : $CHAR1.
            R              : $CHAR1.
            S              : $CHAR4.
            T              : $CHAR1.
            U              : $CHAR1.
            V              : $CHAR1.
            W              : $CHAR1.
            X              : $CHAR4.
            Y              : $CHAR1.
            Z              : $CHAR1. ;
  output;
end;
datalines;
'C:\Users\x\test\H20154.csv'
'C:\Users\x\test\H20154.csv'
'C:\Users\x\test\B20154.csv'
'C:\Users\x\test\H20154.csv'
'C:\Users\x\test\N20154.csv'
;

2 REPLIES 2
Reeza
Super User

Use the FILENAME option in the infile statement. You should also make sure it's long enough to hold the path.

 

 

length filename $256.;

infile dummy filevar=fil2read filename=filename end=done DSD
        DLM=';';

source=filename;

 

This post may be of interest to you, especially if you have header rows in your files.

 

https://communities.sas.com/t5/SAS-Communities-Library/How-do-I-write-a-macro-to-import-multiple-tex...

ballardw
Super User

You were almost there. Since the FILEVAR is a temporary variable you need to assign it to a datastep variable.

data Import_Import;
   length ReadFile $ 40.;
   infile datalines;
   length fil2read $40;
   input fil2read $;
   infile dummy filevar=fil2read end=done DSD
        DLM=';';
   ReadFile = Fil2read;
   do while ..

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 5521 views
  • 0 likes
  • 3 in conversation