首页 > 解决方案 > SAS-如何在while循环期间将2个数值连接到变量名上

问题描述

我正在尝试通过编写一个循环来绕过大量语句来改进一些 SAS 代码。语句代码块的示例如下:

if rpq_stage_date_01 ne . then stage_date_01 = rpq_stage_date_01; 
if gps_stage_date_01 ne . then stage_date_01 = gps_stage_date_01;
if ldr_stage_date_01 ne . then stage_date_01 = ldr_stage_date_01; 

if rpq_stage_date_02 ne . then stage_date_02 = rpq_stage_date_02; 
if gps_stage_date_02 ne . then stage_date_02 = gps_stage_date_02;
if ldr_stage_date_02 ne . then stage_date_02 = ldr_stage_date_02;

像这样持续14行......

所以代替这些语句,我想我可以做一个while循环来迭代前9个数字并在它们前面插入/连接一个0。不幸的是,零是必要的。

   data test(drop=i);
   num = 0;

   do i=1 to 10; 
       if rpq_stage_date_(num||i) ne . then stage_date_(num||i) = 
   rpq_stage_date_(num||i);

       if gps_stage_date_(num||i) ne . then stage_date_(num||i) = 
   gps_stage_date_(num||i);

       if ldr_stage_date_(num||i) ne . then stage_date_(num||i) = 
   ldr_stage_date_(num||i);


   end; 

   do i = 10 to 14;
       if rpq_stage_date_(i) ne . then stage_date_(i) = rpq_stage_date_(i);
   end;
       if gps_stage_date_(i) ne . then stage_date_(i) = gps_stage_date_(i);
   end;
       if ldr_stage_date_(i) ne . then stage_date_(i) = ldr_stage_date_(i);
   end;

   RUN;
   PROC PRINT data=test; run;


我尝试了一些不同的东西,例如 cat、catx、'||' 操作员。在 python 中,这实现起来会很有趣,但 SAS 对我的绿色 SAS 本身来说不够灵活。有人甚至不再使用SAS了吗?我很感兴趣!

任何帮助是极大的赞赏!干杯。

标签: stringsasappendconcatnumeric

解决方案


这段代码

if rpq_stage_date_01 ne . then stage_date_01 = rpq_stage_date_01; 
if gps_stage_date_01 ne . then stage_date_01 = gps_stage_date_01;
if ldr_stage_date_01 ne . then stage_date_01 = ldr_stage_date_01; 

可以换成

stage_date_01 = coalesce(ldr_stage_date_01,gps_stage_date_01,rpq_stage_date_01);

您可以定义数组以通过一次遍历数组来处理此问题。

Array RPQ[*] RPT_STAGE_DATE:;
array gps[*] gps_stage_date:;
array ldr[*] ldr_stage_date:;
array stg[*] stage_date_01-stage_date_10;
do I = 1 to dim(stg);
   stg[i] = coalesce(ldr[I],gps[I],rpq[I]);
   end;

推荐阅读