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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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