BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.

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!

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisHemedinger
Community Manager

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:

flow.png

Copy Files task might look like:

 

copyfile.png

 

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

SAS For Dummies 3rd Edition! Check out the new edition, covering SAS 9.4, SAS Viya, and all of the modern ways to use SAS!

View solution in original post

4 REPLIES 4
Shmuel
Garnet | Level 18

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.

Reeza
Super User

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. 

 

 

ChrisHemedinger
Community Manager

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:

flow.png

Copy Files task might look like:

 

copyfile.png

 

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

SAS For Dummies 3rd Edition! Check out the new edition, covering SAS 9.4, SAS Viya, and all of the modern ways to use SAS!

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 2105 views
  • 1 like
  • 4 in conversation