如何用数据集中的SAS中的另一行值替换值

How to replace value with Another Row value in SAS from Data Set

如果值为1如何替换为下行值"Weight"找到下图当前数据集和所需的最终输出,

请任何人帮助如何获得最终所需的数据集

将该额外行视为单独的数据集。分几步说清楚。

data weights;
  set have;
  where emp_id='weight';
  rename test1-test4 = weight1-weight4 ;
  keep test1-test4 ;
run;
data want ;
  if _n_=1 then set weights;
  set have;
  array t test1-test4;
  array w weight1-weight4;
  if emp_id = 'weight' then delete;
  do index=1 to dim(t);
     t[index]=t[index]*w[index];
  end;
  drop weight1-weight4;
run;

如果您想通过保留体重观察来搞乱您的结果,您可以更改 IF THEN 逻辑,以便 T[*] 的值仅在观察是真实员工数据但体重观察不是时更新已删除。

  if emp_id ne 'weight' then do index=1 to dim(t);
     t[index]=t[index]*w[index];
  end;

如果需要,您可以在不创建单独数据集的情况下执行此操作,但不确定它是否真的有用。

我已将上述数据集拆分为两个数据集,分别用于 employee_id 和员工体重

data two;
retain type 'n';
input GROUP $ CODE $;
fmtname=cats(trim(group)||'f');
start=1; 
label=code;
cards;
TEST1 1.412
TEST2 0.207
TEST3 0.207
TEST4 0.207

proc format cntlin=two;
run;

data one;
input emp_id TEST1 TEST2 TEST3 TEST4;
format test1 test1f. test2 test2f. test3 test3f. test4 test4f.;
cards;
10850 0 1 0 1
10851 1 0 0 0
10852 0 0 1 0
10853 0 0 0 0;

上面的精确输出,没有拆分数据集

data have;
input id $ test1-test4;
cards;
1 0 0 0 1
2 1 0 0 0
3 0 0 1 0
4 0 0 0 0
weight 1.412 0.207 0.207 0.207;
data want;
set have;
if _n_=1 then set have(keep=test1-test4 
rename=(test1-test4=t1-t4)) point=nobs nobs=nobs;
array x{*} test1-test4;
array y{*} t1-t4;
do i=1 to dim(x);
if x{i} then x{i}=y{i};
end;
drop i t1-t4;
run;