【问题标题】:Grab string from the last cell that isn't missing in SAS从 SAS 中没有丢失的最后一个单元格中获取字符串
【发布时间】:2019-04-23 04:48:37
【问题描述】:

HAVE 是 pubs 上的作者数据集:

^注意 auth50 是数据集中的最后一个变量。

WANT 是一个数据集,其中HAVE 每次观察的第一作者和最后作者是他们自己的变量:

我在数据步骤中执行以下操作以创建 lastauth...

array auths {*} auth1:auth50;
do i=1 to dim(auths);
    if auths{1} NE " " then firstauth = auths{1};
    if auths{i}  = " " then lastauth = auths{i-1}; 
else lastauth = auths{i};
end;

...我认为会迭代地覆盖lastauth,直到遇到最后一个authX变量,但代码不会检索最后一个非缺失值。对我的误解有什么想法吗?谢谢!

【问题讨论】:

    标签: arrays loops sas data-manipulation


    【解决方案1】:

    仔细检查逻辑。您想在缺少 FIRST 时设置 FIRST 的值,并在不缺少电流时设置 LAST 的值。

    array auths auth1-auth50;
    firstauth=auths(1);
    do i=1 to dim(auths);
      if missing(firstauth) then firstauth = auths{i};
      if not missing(auths{i}) then lastauth = auths{i}; 
    end;
    

    注意:DO 循环之前的额外赋值是强制 SAS 定义新变量,否则第一次使用将在 IF 条件下。如果您已经有定义 FIRSTAUTH 的 LENGTH 或其他语句,则不需要额外的赋值语句。

    或者跳过数组,只使用 COALESCEC() 函数来查找第一个值。并且只需颠倒变量的顺序,也可以使用它来查找最后一个值。

    firstauth = coalescec(of auth1-auth50);
    lastauth = coalescec(of auth50-auth1);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-14
      • 1970-01-01
      相关资源
      最近更新 更多