简体   繁体   English

如何从SAS中设置的日期获取系列的最后一个非空列

[英]how to get last non empty column of a series from a date set in SAS

I'm working on a SAS application and I've a data set as 我正在处理SAS应用程序,并且有一个数据集

dim_point
rk | ID    | name  | value_0 | value_1 | value_2 | value_3 | value_4
1  | one   |  one  |  val_0  |  val_1  |  val_2  |  val_3  |   .
2  | two   |  two  |  val_0  |  val_1  |  val_2  |    .    |   .
3  | three | three |  val_0  |    .    |    .    |    .    |   .
4  | four  | four  |  val_0  |  val_1  |    .    |    .    |   .

I want to get other columns and last non empty column of value as 我想获取其他列和最后一个非空列的值作为

want
rk | ID    | name  |  value
1  | one   |  one  |  val_3
2  | two   |  two  |  val_2
3  | three | three |  val_0
4  | four  | four  |  val_1

code that I'm trying to do is 我想做的代码是

proc sql noprint;
create table want as
select rk, ID, name, name as value
from dim_point;

update want 
set value = "";
quit;

I don't know how I can update value column with last non empty column value of value_ series? 我不知道如何使用value_系列的最后一个非空列值更新值列?

Use coalesce in reverse order: 反向使用coalesce

set value = coalesce(value_4,value_3,value_2,value_1,value_0);

You might need to use coalescec instead for character variables. 您可能需要对字符变量使用coalescec

Whilst the coalesce solution is fine when you know all the columns, there may be situations where they can either vary, or are significantly more of them. 当您了解所有列时,虽然coalesce解决方案很好,但在某些情况下它们可能会有所不同,也可能会更多。

In that case, you can use an array : 在这种情况下,您可以使用数组:

data want ;
  set have ;
  array _v{*} value_: ; /* colon operator is a wildcard, loaded in the same order as the input dataset */

  /* Loop backwards over the array, stop when we find a non-missing value */
  do i = dim(_v) to 1 by -1 until (not missing(value)) ;
    if not missing(_v{i}) then value = _v{i} ;
  end ;

  drop i ;
run ;

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

相关问题 如何在 ms 访问中获取最后一个非空列 - how to get the last non empty column in ms access 如何从日期列中获取上个月的最后日期? - How to get from a date column the last date of the previous month? 如何获取层次结构的最后一个非空值? - How to get the last non empty value of a hierarchy? 如何从 postgresql 中的当前日期生成最后 7 个日期的系列? - How to generate series of last 7 date from current date in postgresql? 按列获取最后一个非 null 值,其中列按日期排序 - Get last non null value columnwise where column is sorted by date 如何获取SAS中日期列的第二最大和第三最大记录 - How to get the second max and third max records of date column in sas 如何将一个日期列分为两列并获取从开始日期到最后日期的数据 - How to split one date column into two columns and get data from the start date to last date Google表格中的查询功能 - 根据另一列获取列中的非空最后一个单元格 - Query function in Google Sheets - Get non-empty last cell in a column depending on another column 从sql_varient列获取非空数据 - Get the non empty data from a sql_varient column 如何在Oracle中获取从当前日期到最近6个季度的当前日期? - How to get current date to last 6 quarter from date and to date in Oracle?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM