DATA Step, Macro, Functions and more

Insert filename into table using datastep

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 83
Accepted Solution

Insert filename into table using datastep

I am using the datastep to read in a text file.  Each text file has a distinct filename.  I'm unsure how to bring the distinct filename for each record into the table?

Here is my code:

libname YOKOGAWA "D:\***\*****\****\YOKOGAWA";

FILENAME NORM 1 \\**\***\SAS DATA NORMALIZER 1\NORMALIZER 1*.txt ;

data YOKOGAWA.NORM_1_YOKOGAWA

%let _EFIERR_ = 0; /*set the ERROR detection macro variable/*

infile NORM1 delimiter = ',' MISSOVER DSD lcrecl=32767

firstobs=20 EOV=SKIP;

informat yok_dttm ANYDTDTM23.;

informat fpm best32.;

input @; /*if skip then skip=0; else do; */

if skip then do;

skip=0;

input #19;

end;

else do;

     input

          yok_dttm

          fpm

;

output;

end;

if _ERROR_ then call symputx('_EFIERR_',1);

RUN;


Accepted Solutions
Solution
‎11-21-2014 11:08 AM
Super User
Posts: 11,343

Re: Insert filename into table using datastep

On the infile statement add:

FileName= TempFileName

This add a temporary variable that can be referenced but is not kept in the data set.

and in the body of the code

File = TempFileName;

this keeps the name of the file.

View solution in original post


All Replies
Solution
‎11-21-2014 11:08 AM
Super User
Posts: 11,343

Re: Insert filename into table using datastep

On the infile statement add:

FileName= TempFileName

This add a temporary variable that can be referenced but is not kept in the data set.

and in the body of the code

File = TempFileName;

this keeps the name of the file.

Frequent Contributor
Posts: 83

Re: Insert filename into table using datastep

One more question,

The filename includes the whole directory which is over 70 characters long.  The results only display the first 8 characters.  I tried using the informat to bring in all characters, but it does not work.  Ideally, I would like to use something like the substr function and only display the last few characters of the filename.

Super User
Posts: 11,343

Re: Insert filename into table using datastep

Forgot to mention that you need to specify the length of the file variable before use

length file $ 70;

Frequent Contributor
Posts: 83

Re: Insert filename into table using datastep

Ok, I lied that wasn't my last question Smiley Happy


I have added

infile filename = tempfilename;

length file $70.;

file=tempfilename;

In the log it lists the whole directory as the filename now.  However, in the sas dataset file is only showing 8 characters.  I tried adding "file" to the informat and format statements with 70 characters, but no success.  What am I missing?

Frequent Contributor
Posts: 83

Re: Insert filename into table using datastep

Sorry found what I was missing.

I was missing the length for tempfilename.

Thanks you so much for your help.

Lori

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 858 views
  • 3 likes
  • 2 in conversation