Desktop productivity for business analysts and programmers

Getting the file name from importting wizard

Accepted Solution Solved
Reply
anonymous_user
Posts: 0
Accepted Solution

Getting the file name from importting wizard

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!


Accepted Solutions
Solution
Accepted by anonymous_user
‎12-21-2017 07:53 AM
Community Manager
Posts: 3,357

Re: Getting the file name from importting wizard

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

View solution in original post


All Replies
Trusted Advisor
Posts: 1,826

Re: Getting the file name from importting wizard

Posted in reply to anonymous_user

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.

anonymous_user
Posts: 0

Re: Getting the file name from importting wizard

SAS Enterprise Guide 4.3
Super User
Posts: 22,853

Re: Getting the file name from importting wizard

Posted in reply to anonymous_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. 

 

 

Solution
Accepted by anonymous_user
‎12-21-2017 07:53 AM
Community Manager
Posts: 3,357

Re: Getting the file name from importting wizard

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

☑ This topic is solved.

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

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