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

I haven't used SAS in a while so this is probably a simple request, but I am having issues creating the dataset I want.

 

data have1;
input id group $;
cards;
1 a
1 b
1 c
;
run;

data have2;
input id samplevalue;
cards;
1 0.25
1 0.50
1 0.75
;
run;

data want;
input id samplevalue group $;
cards;
1 0.25 a
1 0.50 a
1 0.75 a
1 0.25 b
1 0.50 b
1 0.75 b
1 0.25 c
1 0.50 c
1 0.75 c
;
run;

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
veronicasalmaso
Obsidian | Level 7

Hello! You can do so with a simple proc sql.

Select the variables you want from each dataset and merge the datasets by a variable that both datasets have (in this case that's the id)

 

proc sql;
    create table want as
    select a.id, b.samplevalue, a.group
    from have1 as a, have2 as b
    where a.id = b.id;
quit;

Hope this helps!

View solution in original post

3 REPLIES 3
ballardw
Super User

Not the only way. Proc SQL will allow joins on multiple values of matching variables where a data step merge typically doesn't yield the result desired when there are multiples of BY variables in both data sets.

 

proc sql;
   create table want as
   select a.*,b.samplevalue
   from have1 as a
        full join
        have2 as b
        on a.id=b.id
   order by a.id, a.group, b.samplevalue
   ;
quit;
veronicasalmaso
Obsidian | Level 7

Hello! You can do so with a simple proc sql.

Select the variables you want from each dataset and merge the datasets by a variable that both datasets have (in this case that's the id)

 

proc sql;
    create table want as
    select a.id, b.samplevalue, a.group
    from have1 as a, have2 as b
    where a.id = b.id;
quit;

Hope this helps!

JJ_83
Obsidian | Level 7

Thank you so much, this is exactly what I needed and it works perfectly!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 3 replies
  • 349 views
  • 2 likes
  • 3 in conversation