Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

modify multiple observations in a by variable

Tags:

sas

data a1

col1 col2 flag
a    2    .
b    3    .
a    4    .
c    1    .

For data a1, flag is always missing. I want to update multiple rows using a2.

data a2

col1 flag
a    1

Ideal output:

col1 col2 flag
a    2    1
b    3    .
a    4    1
c    1    .

But this doesn't update all the records in by statement.

data a1;
modify a1 a2;
by col1;
run;

Question edited

Actually a1 is a very large data set on server. Hence I prefer to modify it (if possible) instead of creating a new one. Otherwise I have to drop previous a1 first and copy a new a1 from local to server, which will take much more time.

like image 236
Lovnlust Avatar asked Dec 15 '25 12:12

Lovnlust


1 Answers

If you want to do this with MODIFY, you have to loop over the modify dataset in some fashion or it will only replace the first row (because the other dataset will then run out of records - normally this behaves like merge, where once it finds a match it advances to next record). Here's one option - there are others.

data a1(index=(col1));
  input col1 $ col2 flag;
datalines;
a    2    .
b    3    .
a    4    .
c    1    .
;;;;
run;

data a2(index=(col1));
  col1='a'; 
  flag=1;
run;

data a1;
    set a2(rename=flag=flag2);
    do _n_ = 1 to nobs_a1;
      modify a1 key=col1 nobs=nobs_a1;
      if _iorc_=0 then do;
        flag=flag2;
        replace;
      end;
    end;
    if _iorc_=%sysrc(_DSENOM) then _error_=0;
run;
like image 171
Joe Avatar answered Dec 18 '25 07:12

Joe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!