简体   繁体   English

通过在执行循环 SAS 中附加来创建列表

[英]Create list by appending in do loop SAS

I have a macro variable我有一个宏变量


    %let dates = (200101, 200102, 200103);

How do I create this list?如何创建此列表?


    var_out = pop_200101 pop_200102 pop_200103

I can loop through and select the date:我可以循环通过 select 日期:


    %do i=1 %to %sysfunc(countw(%superq(dates),%str(,)));
        %let this_date = %scan(&dates., &i.);

and I can concatenate:我可以连接:


    catt(pop,_&this_date.);

but I don't know how to actually put this into a list.但我不知道如何将其实际放入列表中。

Edit: additional info编辑:附加信息

I have some data我有一些数据


    data example;
     input Q40_ret_200101 Q40_ret_200102 Q40_ret_200103;
     datalines;
     4 6 .
     . 8 9
     3 7 4
     2 3 . 
     ;
     run;

I am trying to do我正在尝试做


    proc summary
     data = example nway;
     var Q40_ret_200101 Q40_ret_200102 Q40_ret_200103;
     output out = count
     N = pop_200101 pop_200102 pop_200103;
    run;

However in reality my dataset is much larger and it is no feasible for me to write in all the variables.然而实际上我的数据集要大得多,我无法写入所有变量。 I have managed to get a list of my variable names for the var statement.我设法获得了 var 语句的变量名列表。


    PROC CONTENTS 
     DATA = example
     OUT = VAR_NAMES (KEEP = NAME) NOPRINT;
    RUN;

    /*read in variable names and use substr to test if starts with q40_ret*/
    DATA PARSE;
     SET VAR_NAMES;
     WHERE (SUBSTR(NAME, 1,8) = 'Q40_ret_');
    RUN; 

    DATA _NULL_;
     FILE 'C:';
     SET PARSE END = FINIS;
     IF _N_ = 1 THEN PUT '%LET VAR_LIST = ';
     PUT NAME;
     IF FINIS THEN DO;
     CALL SYMPUT('NUM' , COMPRESS(_N_));
     PUT ';';
     PUT 'RUN;';
     END;
    RUN;

    /*Next, the file ‘BUILD‘ is read back into the SAS program using a %INCLUDE statement.*/
    %include 'C:\BUILD';

So far my proc summary statement looks like this:到目前为止,我的 proc 摘要语句如下所示:


    proc summary
     data = example nway;
     var &var_list;
     output out = pop (drop = _TYPE_ _FREQ_)
     N =;
    run;

But I still need to generate and state the output variable names但是我仍然需要生成 state 和 output 变量名

A doSubL side session program can compute and populate the value for the var_out macro symbol. doSubL端 session 程序可以计算和填充var_out宏符号的值。 The benefit of using %sysfunc(doSubL( is that the side program does not create a step boundary in the invoking session.使用%sysfunc(doSubL(的好处是边程序在调用 session.

Example:例子:

The construct of the dates macro variable value can also be used as an array initialization in the doSubL code. dates宏变量值的构造也可以用作doSubL代码中的数组初始化。

%let dates = (200101, 200102, 200103);

%let rc=%sysfunc(dosubl(
  data _null_;
    array dates (%sysfunc(countw(&dates))) &dates;
    do index = 1 to dim(dates);
      length list $200;
      list = catx(' ', list, cats('pop_',dates(index)));
    end;
    call symput('var_out',trim(list));
  run;
));

%put &=var_out;

---------- LOG ----------
VAR_OUT=pop_200101 pop_200102 pop_200103

If you can change your list slightly by removing the parentheses and commas this is quite easy.如果您可以通过删除括号和逗号来稍微更改您的列表,这很容易。 Alternatively you could use COMPRESS() to remove the parenthesis and commas and then use this solution.或者,您可以使用 COMPRESS() 删除括号和逗号,然后使用此解决方案。

%macro prefix(prefix,list);
  %local i bit;
  %let i=1;
  %let bit=%sysfunc(scanq(&list,&i,%str( )));
  %do %while(%length(&bit));
&prefix.&bit
    %let i=%eval(&i+1);
    %let bit=%sysfunc(scanq(&list,&i,%str( )));
  %end;
%mend prefix;

%let dates = (200101, 200102, 200103);

%let want = %prefix(pop_, 200101 200102 200103);

%put &want;

Via: http://www.datasavantconsulting.com/roland/Spectre/utilmacros/prefix.sas通过: http://www.datasavantconsulting.com/roland/Spectre/utilmacros/prefix.sas

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM