BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
asinusdk
Calcite | Level 5
Hi, I wonder what kind of joining this kind of table it is.
Could you please let me know proc sql code?



data a; input year id v1; cards; 1 1 23 2 1 25 3 1 26 1 3 27 2 3 28 ; data b; input year id v2 v3; cards; 1 1 1 34 2 1 0 35 3 1 1 245 1 2 0 234 2 2 1 325 3 2 0 3 1 3 1 25 2 3 0 23 3 3 1 34 ;
The expected table looks like this.

yearidnumbv1v2
1123134
2125035
31261245
1327125
2328023
33.134


+addition: I want to extract all the information (v2 and v3 for all years 1-3) from table b

(even though there is one or two rows in table a) for only ids existing in table a. 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20
data a;
input year  id	v1;
cards;
1 1 23
2 1 25
3 1 26
1 3 27 
2 3 28
;

data b;
input year id v2 v3;
cards;
1 1 1 34 
2 1 0 35
3 1 1 245
1 2 0 234
2 2 1 325
3 2 0 3
1 3 1 25
2 3 0 23
3 3 1 34
;

data want;
if 0 then merge a b;
call missing(v1);
merge a(in=a) b(in=b);
by id;
if a and b;
run;

View solution in original post

8 REPLIES 8
PaigeMiller
Diamond | Level 26

Because it is not clear why you include the last row in your desired output table, this desired output doesn't correspond to any built in JOIN in PROC SQL. If you can clearly state a reason why that last row is included, then it becomes easier to figure out how it can be programmed.

--
Paige Miller
asinusdk
Calcite | Level 5

Thanks for your response.

I want to extract all the information (all years 1-3) from table b (even though there
is one or two rows in table a) for only ids existing in table a.

novinosrin
Tourmaline | Level 20
data a;
input year  id	v1;
cards;
1 1 23
2 1 25
3 1 26
1 3 27 
2 3 28
;

data b;
input year id v2 v3;
cards;
1 1 1 34 
2 1 0 35
3 1 1 245
1 2 0 234
2 2 1 325
3 2 0 3
1 3 1 25
2 3 0 23
3 3 1 34
;

data want;
if 0 then merge a b;
call missing(v1);
merge a(in=a) b(in=b);
by id;
if a and b;
run;
novinosrin
Tourmaline | Level 20

Hi @asinusdk  Honestly the above is just to see data and attempt to get the output using some code I didn't pay attention to logical business need or objective as the question didn't reflect that. In other words, there is no what & why

asinusdk
Calcite | Level 5

I see... thanks, I should have added the explanation and logic. I'm sorry..

asinusdk
Calcite | Level 5

what if there are more variable in data a and I want to include these variables in the expected outcome?

novinosrin
Tourmaline | Level 20

did you try my code?

FreelanceReinh
Jade | Level 19

@asinusdk wrote:

what if there are more variable in data a and I want to include these variables in the expected outcome?


Hi @asinusdk,

 

Try this:

data a;
input year id v1 morevar;
cards;
1 1 23 9
2 1 25 8
3 1 26 7
1 3 27 6
2 3 28 5
;

data b;
input year id v2 v3;
cards;
1 1 1 34 
2 1 0 35
3 1 1 245
1 2 0 234
2 2 1 325
3 2 0 3
1 3 1 25
2 3 0 23
3 3 1 34
;

proc sql;
create table want as
select b.year, b.id, v1 as numb, v2 as v1, v3 as v2, morevar
from a right join b
on a.id=b.id & a.year=b.year
where exists (select * from a where a.id=b.id)
order by id, year;
quit;

Edit:

Slightly shorter with

from a natural right join b

and without the ON clause.

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
  • 8 replies
  • 1133 views
  • 0 likes
  • 4 in conversation