BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
dapenDaniel
Obsidian | Level 7

Hi,

 

I have two datasets as below.

data have1;
input ID $ Var1;
datalines;
AA 0
AA 0 
BB 0  
run;
data have2;
input ID $ Var1;
datalines;
AA 1
run;

I would like to replace all id=AA in have1 with the value of id=AA in have2. The expected results are below.

 

 

ID       Var1

AA        1

AA        1

BB        0

 

I used the following program but it can only replace the value of first AA with 1 and that of the second one is still 0

proc sort data=have1 out=have11;
by classD;
run;

proc sort data=have2 out=have21;
by classD;
run;

data merged;
merge have11 (in=q) have21;
by id;
if q;
run;

How can I get the expected results? Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20
proc sort data=have1 out=have11;
by id;
run;

proc sort data=have2 out=have21;
by id;
run;

data merged;
merge have11(in=q) have21(in=z);
by id;
if q;
var1=z;
run;

View solution in original post

7 REPLIES 7
novinosrin
Tourmaline | Level 20
proc sort data=have1 out=have11;
by id;
run;

proc sort data=have2 out=have21;
by id;
run;

data merged;
merge have11(in=q) have21(in=z);
by id;
if q;
var1=z;
run;
dapenDaniel
Obsidian | Level 7

Thank you @novinosrin 

dapenDaniel
Obsidian | Level 7

Hi @novinosrin 

 

actually, I have 335 variables after Id and their name is Col2, Col3,...Col336. How can I let SAS know that I would like to keep the value of id=AA in have2? Thanks.

novinosrin
Tourmaline | Level 20

Well your right table i.e the variable names in table name specified on the right will overwrite the values in the left if the names the the same. Is this your concern?

 

In any case, test for like 10-15 vars manually and then you are ought to be more confident 

dapenDaniel
Obsidian | Level 7

Sorry, I did not put it in a clear way. I mean there are 335 variables called Var1, Var2,...Var336 after Id. I have added 5 variables manually and it works (code is below). I am wondering whether there is a way that I can tell SAS to overwrite the value for all these 335 variables, like Var2-Var336=z (I tried it but it did not work).

 

data merged;
merge have11 (in=q) have21 (in=z);
by id;
if q;
Var58=z;
Var65=z;
Var88=z;
Var144=z;
Var203=z;
run;

 

novinosrin
Tourmaline | Level 20

Ah ok, So are the Vars consecutive in sequence?

 

If yes,

array t Var2-Var336; grouping can work

data merged;
merge have11 (in=q) have21 (in=z);
by id;
if q;
array t Var2-Var336;
do over t;
 t=z;
end;
run;

 

dapenDaniel
Obsidian | Level 7

Thanks!

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 7 replies
  • 650 views
  • 0 likes
  • 2 in conversation