如何在sas数据集中保留值?

时间:2018-04-19 17:55:15

标签: sas retain

我有以下数据集

data have;
input pop$  district$ racemajor$;
cards;
    color   Aberdeen    .
    white   Aberdeen    .
    Black   Aberdeen    .
    Asian   Aberdeen    .
    Black   Adelaid  Yes
    Color   Adelaid     .
    white   Adelaid     .
    Asian   Adelaid     .
    White   Bellvill    .
    black   Bellvill    .
    Asian   Bellvill    .
;
run;

基本上我想拖动值'Yes'如果racemajor是'是'对于相应的区域,使其看起来如下

data want;
    color   Aberdeen    .
    white   Aberdeen    .
    Black   Aberdeen    .
    Asian   Aberdeen    .
    Black   Adelaid     Yes
    Color   Adelaid     Yes
    white   Adelaid     Yes
    Asian   Adelaid     Yes
    White   Bellvill    .
    black   Bellvill    .
    Asian   Bellvill    .

我知道我可以使用first.retain语句来执行此操作,我尝试了以下操作。但是,它似乎不起作用。

data want;
set have;
if first.district and racemajor='Yes';
retain racemajor;
run;

2 个答案:

答案 0 :(得分:1)

试试这个,虽然为了安全起见,你应该对分区上的数据进行排序

data NEW;
drop test;
SET HAVE;
by district;
retain test;
if first.district then test = racemajor;
racemajor=test;
run;

答案 1 :(得分:0)

这是更新技巧的好例子,其中主人是空的,你输出所有的障碍物。

data have;
   input (pop district racemajor) ($);
   cards;
    color   Aberdeen    .
    white   Aberdeen    .
    Black   Aberdeen    .
    Asian   Aberdeen    .
    Black   Adelaid  Yes
    Color   Adelaid     .
    white   Adelaid     .
    Asian   Adelaid     .
    White   Bellvill    .
    black   Bellvill    .
    Asian   Bellvill    .
;;;;
   run;
proc print;
   run;
data want;
   update have(obs=0) have;
   by district;
   output;
   run;
proc print;
   run;