DATA Step, Macro, Functions and more

Most easy way to do one-to-many in SAS, (DATA steep)

Reply
Contributor
Posts: 43

Most easy way to do one-to-many in SAS, (DATA steep)

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?

Super User
Posts: 7,854

Re: Most easy way to do one-to-many in SAS, (DATA steep)

Posted in reply to Angel_Saenz

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 5,516

Re: Most easy way to do one-to-many in SAS, (DATA steep)

Posted in reply to Angel_Saenz

I agree with @KurtBremser, 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.

Super User
Super User
Posts: 7,076

Re: Most easy way to do one-to-many in SAS, (DATA steep)

[ Edited ]
Posted in reply to Angel_Saenz

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;

 

Trusted Advisor
Posts: 1,022

Re: Most easy way to do one-to-many in SAS, (DATA steep)

Posted in reply to Angel_Saenz

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.

Ask a Question
Discussion stats
  • 4 replies
  • 98 views
  • 2 likes
  • 5 in conversation