BookmarkSubscribeRSS Feed
shlomiohana
Obsidian | Level 7

Hi, everyone.

I need help to replace rows in an existing table.

 

dataset A:

Branchtarget_1target_2target_3
1108010
2125038
3303040
4101080
5312940
6442630
7701317
8204040

 

dataset B:

Branchtarget_1target_2target_3
1222266
4441244
6304030

 

I want dataset A look like:

 

Branchtarget_1target_2target_3
1222266
2125038
3303040
4441244
5312940
6304030
7701317
8204040

 

Any suggestions?

Thanks.

4 REPLIES 4
Shmuel
Garnet | Level 18

data want;

   update datasetA 

              datasetB;

     by branch;

run;

mklangley
Lapis Lazuli | Level 10
proc sql;
    create table want as
    select a.branch
          ,case when b.branch is not null then b.target1 else a.target1 end as target1
          ,case when b.branch is not null then b.target2 else a.target2 end as target2
          ,case when b.branch is not null then b.target3 else a.target3 end as target3
    from a left join b on a.branch = b.branch
    ;
quit;
shlomiohana
Obsidian | Level 7

Thank you very much, it is possible to get a solution including a loop?

mklangley
Lapis Lazuli | Level 10

Do you mean a loop through the target1target2target3 variables?

%macro loop;
    proc sql;
        create table want as
        select a.branch
            %do i = 1 %to 3;
              ,case when b.branch is not null then b.target&i else a.target&i end as target&i
            %end;
        from a left join b on a.branch = b.branch
        ;
    quit;
%mend;
%loop;

Or are you envisioning some other sort of loop?

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1355 views
  • 4 likes
  • 3 in conversation