I have 2 datasets:
data dataset1;
INPUT VAR1 VAR2 $;
cards;
1 A
1 AB
1 ABC
2 A
2 AB
2 ABC
;
RUN;
data dataset2;
INPUT VAR1 VAR2 $;
cards;
1 Z
1 ZY
1 ZYX
2 Z
2 ZY
2 ZYX
;
RUN;
I need this result (one-to-many merge):
VAR1 | VAR2 | VAR3 |
1 | A | Z |
1 | A | ZY |
1 | A | ZYX |
1 | AB | Z |
1 | AB | ZY |
1 | AB | ZYX |
1 | ABC | Z |
1 | ABC | ZY |
1 | ABC | ZYX |
2 | A | Z |
2 | A | ZY |
2 | A | ZYX |
2 | AB | Z |
2 | AB | ZY |
2 | AB | ZYX |
2 | ABC | Z |
2 | ABC | ZY |
2 | ABC | ZYX |
Im doing this with this proc:
PROC SQL;
SELECT DATASET1.VAR1, DATASET1.VAR2, DATASET2.VAR2 AS VAR3
FROM DATASET1 INNER JOIN DATASET2 ON DATASET1.VAR1 = DATASET2.VAR1;
RUN;
But I want to know the most easy way to do it with SAScode, with DATA STEP
How can I do it?
This is a many-to-many join, not a one-to-many. Creating a cartesian join in a data step is at least complicated. Stay with SQL.
I agree with @Kurt_Bremser, there is no simple DATA step method for this. It is possible you would consider this SQL variation slightly simpler. You could change the second line of the SELECT statement, making it:
FROM DATASET1, DATASET2 where DATASET1.VAR1 = DATASET2.VAR1;
Note that PROC SQL ends with a QUIT; statement, not a RUN; statement.
Getting a data step to do a many-to-many join with ID variables can be a pain.
Here is an example doing an INNER JOIN (requires that ID is in both input tables) using POINT= option on SET statements to re-read the data. You could also build up hashes if your BY groups are small enough that you can load all of the records into memory.
data want ;
* count records per id per table ;
* Keep track of first/last record number ;
first1=c1+1;
first2=c2+1;
do until(last.id);
set one(in=in1 keep=id) two(in=in2 keep=id);
by id;
if in1 then do; any1 = 1; c1+1; end;
if in2 then do; any2 = 1; c2+1; end;
end;
* INNER JOIN requires ID to be in both tables ;
if (c1>=first1) and (c2>=first2) then do;
do p1=first1 to c1 ;
set one point=p1 ;
do p2=first2 to c2;
set two point=p2;
output;
end;
end;
end;
* drop variables used for tracking ;
drop first1 first2 c1 c2 ;
run;
SQL is definitely easier than DATA step for this task.
But if you are confident that dataset 2 always has exactly 3 observations for each value of the matching var (var1 in your example), you can make a relatively simple data step, using the POINT= option is a SET DATASET2 statment:
data want (drop=_:);
set dataset1;
by var1;
_group+first.var1;
do p= 3*_group-2 to 3*_group;
set dataset2 (rename=(var2=var3)) point=p;
output;
end;
run;
Now think about the complexities neither dataset has a fixed number of observations per record. You may not want to go there.
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.
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.