Search code examples
sassas-macro

Capture imported file name into a variable in SAS


I'm trying to find a way to capture into a variable the name of a file what I'm importing in SAS as I want to use it to build the name of an exported file. For exemple, I have the file TEST.xlsx in the directory c:\TEMP, what I want to import in SAS and after some manipulation of data to export the result as TEST-01.xlsx. Can someone, please, help me to do that?

Thank you, Dan

PROC IMPORT DATAFILE= "c:\TEMP\*.xlsx" DBMS=xlsx out=TABLE_START  REPLACE;          
RUN;

Solution

  • Just find the name first.

    data _null_;
      length fname $300 ;
      infile "c:\TEMP\*.xlsx" filename=fname;
      input @;
      call symputx('fname',fname);
      stop;
    run;
    

    Then you can use the filename in your IMPORT or other steps.

    PROC IMPORT DATAFILE= "&fname" DBMS=xlsx out=TABLE_START  REPLACE;          
    RUN;