string - 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了吗?我很感兴趣!
任何帮助是极大的赞赏!干杯。
解决方案
这段代码
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;
推荐阅读
- c - 为 Make 添加不同的对象目录
- pytorch - 如何在每批中创建具有不同元素的火炬对角矩阵?
- laravel - 在控制器中返回对象数组后在 Laravel 中查找模型
- c - C Scanf 命令不会运行
- swift - GeoFire Circle 查询 NSException
- ios - SwiftUI 中未触发 onReceive
- python - 在 Python 中使用 BeautifulSoup 抓取谷歌搜索
- r - R - 使用'stat_compare_means'重新格式化ggplot中的P值
- json - 当 KinematicBody2D 存储在 JSON 文件 - Godot 中时,这意味着什么?
- macos - 如何将 VSCode 的集成终端上的 cmd+backspace 功能添加到 Mac 的