Search code examples
sassas-macro

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


Solution

  • 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.