DATA Step, Macro, Functions and more

Using infile with several files (*)

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 122
Accepted Solution

Using infile with several files (*)

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,


Accepted Solutions
Solution
‎06-07-2016 04:07 PM
Trusted Advisor
Posts: 1,117

Re: Using infile with several files (*)

Posted in reply to juanvg1972

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


All Replies
Solution
‎06-07-2016 04:07 PM
Trusted Advisor
Posts: 1,117

Re: Using infile with several files (*)

Posted in reply to juanvg1972

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=...

Super Contributor
Posts: 438

Re: Using infile with several files (*)

Posted in reply to juanvg1972

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.

Super User
Posts: 11,343

Re: Using infile with several files (*)

Posted in reply to juanvg1972

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;

 

Super User
Posts: 3,252

Re: Using infile with several files (*)

Posted in reply to juanvg1972
☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 300 views
  • 4 likes
  • 5 in conversation