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,
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=...
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=...
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.
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;
Check out the FILENAME option on the INFILE statement:
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.