在SAS中使用proc sql通过数组进行简单迭代 [英] Simple iteration through array with proc sql in SAS

查看:157
本文介绍了在SAS中使用proc sql通过数组进行简单迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想遍历列表,并使用列表中的'i'元素运行一个过程,并用结果创建一些名为'i'的表.我已经尝试过从我可以找到的每个资源中获取语法,但是无法使它正常工作.这是一些说明我需要的代码/伪代码.提前非常感谢!

I just want to loop through a list and run a procedure using the 'i'th element in the list, and make some table named 'i' with the result. I've tried the syntax from every resource I can find but can't get this to work. Here's some code/pseudocode that illustrates my need. Many thanks in advance!

array itemlist[*] (100,101,102);

proc sql;
    do i=1 to dim(itemlist);
    create table somelibname.[itemlist(i)] as
        select * from somelibname.sometable
        where item=itemlist(i);
    end;
quit;

推荐答案

您需要使用宏为您编写" SAS代码.

You need to use a macro to "write" the SAS code for you.

这应该做您想要的.它使用空格分隔的值列表,并按照您的代码指定的方式遍历它们.如果您有任何疑问,请发表评论.

This should do what you are looking for. It takes a space delimited list of values, and loops over them doing what your code specifies. Post a comment if you have a question on it.

%macro doit(list);
proc sql noprint;
%let n=%sysfunc(countw(&list));
    %do i=1 %to &n;
        %let val = %scan(&list,&i);
        create table somlib._&val as
            select * from somlib.somtable
            where item=&val;
    %end;
quit;
%mend;

%doit(100 101 102);

请注意,数据集不能以数字开头,因此我将其以'_'开头

Note, data sets cannot start with a number so I have these starting with '_'

这篇关于在SAS中使用proc sql通过数组进行简单迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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