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

Hi,

 

I am doing a datastep with infile. In my infile I have several files with same structure, and with similar name

 

I use:

 

data customers;

infile "/home/aps/source/customers_group*;

...

run;

 

then I read files customer_group1, customer_group2,...., customer_group8 SAS dataset.

 

I would likee to know if is possible to create a field with the name of the file of each rows.

 

For example

 

source= customer_group1 for rows from this file.

 

Perhaps a system var??

 

I don't have any other field in the files to identify this information.

 

Thanks in advance,

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hi @juanvg1972,

 

You can use the FILENAME= option of the INFILE statement to obtain the file name:

data customers;
length fname $300
       source $30; 
infile "/home/aps/source/customers_group*" filename=fname;
source=scan(fname,-1,'/');
...
run;

Variable FNAME is automatically dropped and receives the full path of the currently opened input file, hence the additional assignment statement SOURCE=...

View solution in original post

4 REPLIES 4
FreelanceReinh
Jade | Level 19

Hi @juanvg1972,

 

You can use the FILENAME= option of the INFILE statement to obtain the file name:

data customers;
length fname $300
       source $30; 
infile "/home/aps/source/customers_group*" filename=fname;
source=scan(fname,-1,'/');
...
run;

Variable FNAME is automatically dropped and receives the full path of the currently opened input file, hence the additional assignment statement SOURCE=...

jklaverstijn
Rhodochrosite | Level 12

Have a look at the FILEVAR on the FILENAME statement. It May be what you're lookin for.

 

https://support.sas.com/techsup/technote/ts581.pdf

 

Hope this helps,

Jan.

ballardw
Super User

The option on the INFILE statement is FILENAME. The option creates a temporary variable so you need to assign the result to a datastep variable to become a permanent part of the data set.

 

data new;

  /* the dataset name will be in varaible dsname you need to ensure the length of it and the temporary variable are long enough to hold the path and name */

   length dsname myfile $ 200;

   infile  "/home/aps/source/customers_group*"   filename=myfile;

   dsname = myfile;

run;

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 4 replies
  • 1334 views
  • 4 likes
  • 5 in conversation