具有可变部分的 SAS 文件路径

SAS filepath with variable parts

我正在尝试遍历不同文件的列表并输入它们。

DO Year = 2000 to 2021 by 1;
        filename fileftp ftp year+'.csv.gz' host='ftp.abcgov' 
        cd='/pub/' user='anonymous'
        pass='XXXX' passive recfm=s debug;
        INFILE fileftp NBYTE=n;
END;

我怎样才能在文件名中包含年份? 目前,当我尝试这个 (year+'.csv.gz') 时,它试图将年份错误地识别为一个选项。

您需要为此使用 SAS 宏工具。由于您的文件是压缩文件,您还需要在导入数据之前将其解压缩。

%macro importData;
    %do year = 2000 to 2021;
        filename fileftp ftp "&year..csv.gz" 
            host  = 'ftp.abcgov'
            cd    = '/pub/' 
            user  = 'anonymous'
            pass  = 'XXXX' 
            recfm = s 
            passive 
            debug
        ;

        filename download temp;
        
        /* Download the file to a temporary local space */
        %let rc = %sysfunc(fcopy(fileftp, download));

        /* Unzip the file */
        filename unzip "%sysfunc(pathname(download))" gzip;

        /* Read the data and output it by year */
        proc import 
            file = unzip
            out  = want&year.
            dbms = csv
            replace;
        run;

    %end;
%mend;
%importData;

如果 fcopy 对您不起作用,您可以使用数据步骤将一个文件写入另一个文件。

data _null_;   
   infile fileftp;
   file download;
   input;
   put _INFILE_ ;
run;