Shuail_CR007
Shuail_CR007

Reputation: 312

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

How to replace if the value is 1 with the below row value "Weight" find the below image current data set and the final output required,

please any one help how to get the final required dataset

dataset image

Upvotes: 0

Views: 2097

Answers (3)

Shuail_CR007
Shuail_CR007

Reputation: 312

Exact ouput of the above, Without spliting the data sets

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;

Upvotes: 0

Shuail_CR007
Shuail_CR007

Reputation: 312

I have split the above data set into two data sets for employee_id and employee weight

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;

Upvotes: 0

Tom
Tom

Reputation: 51566

Treat that extra row as a separate dataset. Let's do it in multiple steps to make it clearer.

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 you want to mess up your results by keeping the weight observation you could change the IF THEN logic so that the values of T[*] are only updated when the observation is real employee data but the weight observation is not deleted.

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

You could do this without creating the separate dataset if you want, but not sure it really helps.

Upvotes: 0

Related Questions