SAS:while循环不迭代

时间:2013-12-30 07:00:40

标签: macros while-loop sas do-while

我有一个已保存的csv_files SAS数据集,其中包含以下条目:

name path
aapl F:\Data\aapl.csv
msft F:\Data\msft.csv
ibm F:\Data\ibm.csv
goog F:\Data\goog.csv

然后我使用此SAS数据集找到csv文件并使用以下具有while循环的宏将其导入SAS:

options symbolgen mlogic nomprint;  
/*Leave options on as SAS will issue a message when Do While Loop is false*/

%let dsname = csv_files;                               
%let libto = l;


%Macro get_data(n,index);  

     /*n: is a counting macro variable representing the lower limit of the number of CSV files
 to be read into SAS. Index: is a variable that can be thought of as 
observation number( row number) representing the upper limit 
of the number  of CSV files to be read into SAS.*/

%DO %WHILE (&n <=&index);
data _NULL_;
set &libto..&dsname end=last;
if _N_ =&n then Do;
call symput('path',path);      /*  Get file path from CSV_files*/
call symput('dsn',name);       /* Get dataset name from CSV_files*/                     
if not last  then index+1;     /* Create macro variable Index */
else if last then call symput ('index',index);  
End;
run;

proc import datafile= "&path"             
     out= &libto..&dsn
     dbms=dlm
     replace;
     delimiter=","  ;            
     getnames=yes;
run;
%End;

%Mend get_data;

/* Macro Call*/
%get_data(1,4);

当我执行此宏时,我总是迭代第一次观察(在本例中为AAPL)。它只是一遍又一遍地导入aapl.csv文件......出了什么问题?

更新

根据Dmitry Shopin的建议,我将while更改为do循环:

%let dsname = csv_files;                                                   
%let libto = l;

%Macro get_data(n,index);       
%let dsname = csv_files;                                                   
%let libto = l;

%do i=&n %to &index;
data _NULL_;
set &libto..&dsname end=last;
if _N_ =&i then Do;
call symput('path',path);      /*  Get file path from CSV_files*/
call symput('dsn',name);       /* Get dataset name from CSV_files*/                     
if not last  then index+1;     /* Create macro variable Index */
else if last then call symput ('index',index);  
End;
run;

proc import datafile= "&path"             
     out= &libto..&dsn
     dbms=dlm
     replace;
     delimiter=","  ;            
     getnames=yes;
run;
%End;
%End;

%Mend get_data;

/* Macro Call*/
%get_data(1,4);

我得到的错误如下:

ERROR 180-322: Statement is not valid or it is used out of proper order.

我在代码中获得了多行,但是从我的CSV文件中生成了4个SAS数据集中的最后3个。

我也在代码中收到&dsn的错误:

ERROR 22-322: Syntax error, expecting one of the following: ;, (, DATAFILE, DATATABLE, DBMS,
          DEBUG, FILE, OUT, REPLACE, TABLE, _DEBUG_.

3 个答案:

答案 0 :(得分:2)

也许,这是因为你不会在每次迭代时递增你的&amp; n宏变量?我建议更换你的

%DO %WHILE...

循环

 %DO i=&n %TO &index;
...
if _N_=&i then ...;

更新

这就是我的运行方式。它与您的问题完全相同,但是:

- 我添加了数据步骤以创建csv_files数据集

- 我删除了第二个%END语句

- 添加LIBNAME以分配l库

-F F:-drive到C:-drive在路径中(因为我没有F-drive):

libname l 'C:\Data';

data l.csv_files;
    length name $4 path $20;
    input name $ path $;
datalines;
aapl C:\Data\aapl.csv
msft C:\Data\msft.csv
ibm C:\Data\ibm.csv
goog C:\Data\goog.csv
;
run;

%let dsname = csv_files;                                                   
%let libto = l;

%Macro get_data(n,index);       
%let dsname = csv_files;                                                   
%let libto = l;

%do i=&n %to &index;
    data _NULL_;
        set &libto..&dsname end=last;
        if _N_ =&i then Do;
            call symput('path',path);      /*  Get file path from CSV_files*/
            call symput('dsn',name);       /* Get dataset name from CSV_files*/                     
            if not last  then index+1;     /* Create macro variable Index */
            else if last then call symput ('index',index);  
        End;
    run;

    proc import datafile= "&path"             
        out= &libto..&dsn
        dbms=dlm
        replace;
        delimiter=","  ;            
        getnames=yes;
    run;
%End;

%Mend get_data;

/* Macro Call*/
%get_data(1,4);

答案 1 :(得分:0)

如果您只想将它​​们读入SAS数据集,可以尝试使用FILEVAR=语句中的infile选项。当您知道路径时,FILEVAR可以读取多个外部文件。

data files;
    length path $40;
    input name $ path $;
    datalines;
    sample1 C:\sasdata\sample1.csv 
    sample2 C:\sasdata\sample2.csv
    test C:\Cstuff\test.csv
    ;
run;

data want(drop=name);
     set files;
     infile dummy filevar=path DLM=',' length=reclen end=done missover;
     do while (not done);
        input A B C;
        output;
     end;
run;

答案 2 :(得分:0)

在使用代码时,我找到了一种方法来使用while来获取在SAS中导入的所有四个csv文件。我不知道为什么这相对于我之前使用DOWhile

options symbolgen mlogic nomprint;  


%let dsname = csv_files;                                                   
%let libto = l;
libname l 'C:\SASDATA\';

%Macro get_data(n,index);       

%IF &n <= &index %then %Do;

%DO %WHILE (&n <=&index);

data _NULL_;
set l.csv_files end=last;
if _N_ =&n then Do;
call symput('path',Path);                /*  Get file path from Datalog*/
call symput('dsn',Name);       /* Get dataset name from Datalog*/                       
if not last  then index+1;                 /* Create macro variable Index */
else if last then call symput ('index',index);  
End;
run;

proc import datafile= "&path"             
     out= &libto..&dsn
     dbms=dlm
     replace;
     delimiter=","  ;            
     getnames=yes;
run;


%End;
%End;

Data _NULL_;
set &libto..&dsname;
%IF &n gt &Index %then %Put ERROR Ending execution: Check values of n and Index (n must be less than or equal to index);
run;
%Mend get_data;

/* Macro Call*/

%get_data(1,1,4);