BookmarkSubscribeRSS Feed
Angel_Saenz
Quartz | Level 8

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):

VAR1VAR2VAR3
1AZ
1AZY
1AZYX
1ABZ
1ABZY
1ABZYX
1ABCZ
1ABCZY
1ABCZYX
2AZ
2AZY
2AZYX
2ABZ
2ABZY
2ABZYX
2ABCZ
2ABCZY
2ABCZYX

 

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?

4 REPLIES 4
Astounding
PROC Star

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.

Tom
Super User Tom
Super User

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;

 

mkeintz
PROC Star

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.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

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!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 816 views
  • 2 likes
  • 5 in conversation