SAS:做while循环不迭代 [英] SAS: Do while loop not iterating

查看:37
本文介绍了SAS:做while循环不迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a saved csv_files SAS Dataset with the following entries:

name path
aapl F:Dataaapl.csv
msft F:Datamsft.csv
ibm F:Dataibm.csv
goog F:Datagoog.csv

然后我使用这个 SAS 数据集来定位 csv 文件,并使用以下具有 while 循环的宏将它们导入 SAS:

I then use this SAS dataset to locate the csv files and import them into SAS using the following Macro which has a while loop:

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 文件....有什么问题?

When I execute this Macro I always iterate over the first observation (in this case AAPL). It simply imports the aapl.csv file over and over....what's wrong?

更新

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

Following the suggestion of Dmitry Shopin I changed my while for a do loop:

%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 个.

I get this for multiple lines in the code but generates the last 3 out of 4 SAS datasets from my CSV files.

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

I also get the error for the &dsn in the code:

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

推荐答案

也许是因为你没有在每次迭代时增加你的 &n 宏变量?我建议更换你的

Maybe, it's because you don't increment your &n macrovariable with every iteration? I would suggest to replace your

%DO %WHILE...

循环

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

更新

这就是我运行的并且有效.它与您问题中的更新代码完全相同,除了:

This is what I run and it worked. It's exactly the same updated code from your question, except:

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

-I added data step to create csv_files dataset

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

-I deleted the second %END statement

-添加 LIBNAME 以分配 l 库

-added LIBNAME to assign l library

-将路径中的 F:-drive 更改为 C:-drive(只是因为我没有 F-drive):

-changed F:-drive to C:-drive in the paths (just because I don't have F-drive):

libname l 'C:Data';

data l.csv_files;
    length name $4 path $20;
    input name $ path $;
datalines;
aapl C:Dataaapl.csv
msft C:Datamsft.csv
ibm C:Dataibm.csv
goog C:Datagoog.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);

这篇关于SAS:做while循环不迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆