用第二行中的值替换第一行 [英] Replacing first row with values in second row

查看:52
本文介绍了用第二行中的值替换第一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据集在每个唯一 ID 的第一行中都有缺失值.我想用第二行中的值替换第一行.

My dataset has missing values in the first row of every unique ID. I want to replace this first row with the values in the second row.

我的直觉告诉我解决方案涉及使用 _N_BY 语句.

My intuition tells me solution involves using _N_ and BYstatement.

我的数据集:

ID  Var1
1   .
1   12
1   23
1   2
2   .
2   266
2   23
2   2
3   .
3   6

我追求的结果:

ID  Var1
1   12
1   12
1   23
1   2
2   266
2   266
2   23
2   2
3   6
3   6

推荐答案

使用两个 SET 语句.第二个 SET 用于领先"处理(与滞后"相反).第二个set语句的数据集和第一个一样,只是偏移了1行(firstobs=2).

Use two SET statements. The second SET is for 'lead' processing (as opposed to 'lag'). The data set of the second set statement is the same as the first one but offset by 1 row (firstobs=2).

data have;
input ID var1;
datalines;
1   .
1   12
1   23
1   2
2   .
2   266
2   23
2   2
3   .
3   6
run;

data want;
  set have;
  by id;

  set have(firstobs=2 keep=id var1 rename=(id=lead1_id var1=lead1_var1));

  if first.id and id=lead1_id then var1=lead1_var1;

  drop lead1_id lead1_var1;
run;

这篇关于用第二行中的值替换第一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆