Hi
Every day I'm importing a .TXT file using the wizard.
I wonder if it is possible for SAS to capture the file name that I chose on the wizard, so that I can insert it in on a @Char variable in a dataset.
Nowadays I am including the file name manually in the table.
Regards!
I think that @Reeza has the best suggestion. As far as I know, the Import Data task does not place the imported file name into any macro or environment variable, so there isn't a way to pull that detail from the wizard.
However, you could adjust your process to upload the text file to SAS, then run a SAS program node to complete the import. The upload process would need to use the Copy Files task, available for SAS Enterprise Guide 4.3 as a custom task. (In v7.13 and later, it's a built-in task that you can find on the Tasks->Data menu.)
Flow would look like:
Copy Files task might look like:
In this example, the destination folder is the WORK directory. The source file is explicitly named in the Copy Files "source" field, so this would change every time. However, you could assign to a macro variable and use that reference here. When the task runs, the source specification is stored in the &_EGCOPYSOURCE macro variable.
And then your code to import can be more generic, like:
data work.air;
length
date 8
air 8 ;
format date monyy5.;
infile "%sysfunc(getoption(work))/*.csv"
lrecl=32767
firstobs=2
encoding="utf-8"
dlm='2c'x
missover
dsd ;
input
date : monyy5.
air : ?? best3. ;
run;
The wildcard on the INFILE means that you don't need to name the specific file you just copied...as long as it's the only CSV in the destination folder (WORK, in this case).
Plaese supply SAS platform and version you are using.
Posting log of the running wizard maybe may help.
If you know the fileref of importing you can achieve the file name.
If the file is the same each time, can you avoid the wizard? Usually a data step code is better anyways because you can make sure the file comes in the same each time. If you can do this, then there's a FILENAME and/or FILEVAR options that can help you get the file name.
If you're using the wizard because its a local file being moved to the server, this won't work, unless you automate a step to upload the file to the server as well and then use the data step method.
I think that @Reeza has the best suggestion. As far as I know, the Import Data task does not place the imported file name into any macro or environment variable, so there isn't a way to pull that detail from the wizard.
However, you could adjust your process to upload the text file to SAS, then run a SAS program node to complete the import. The upload process would need to use the Copy Files task, available for SAS Enterprise Guide 4.3 as a custom task. (In v7.13 and later, it's a built-in task that you can find on the Tasks->Data menu.)
Flow would look like:
Copy Files task might look like:
In this example, the destination folder is the WORK directory. The source file is explicitly named in the Copy Files "source" field, so this would change every time. However, you could assign to a macro variable and use that reference here. When the task runs, the source specification is stored in the &_EGCOPYSOURCE macro variable.
And then your code to import can be more generic, like:
data work.air;
length
date 8
air 8 ;
format date monyy5.;
infile "%sysfunc(getoption(work))/*.csv"
lrecl=32767
firstobs=2
encoding="utf-8"
dlm='2c'x
missover
dsd ;
input
date : monyy5.
air : ?? best3. ;
run;
The wildcard on the INFILE means that you don't need to name the specific file you just copied...as long as it's the only CSV in the destination folder (WORK, in this case).
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.
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.