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

It's time to register for SAS Innovate! Join your SAS user peers in Las Vegas on April 16-19 2024.

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.

SAS Enterprise Guide 4.3
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).

It's time to register for SAS Innovate! Join your SAS user peers in Las Vegas on April 16-19 2024.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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