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:
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.