BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Fcot
Calcite | Level 5

Hi all,

 

I have a question about merging datasets. I'll try to state my problem as clearly as possible.

 

Dataset #1:

Fund         Target         Permno

Fund_A        ABC           10101

Fund_B        ABC           10101

Fund_C       ABC            10101

Dataset #2

Target        Permno        Return         Date

ABC             10101            0.01              1

ABC             10101            0.02              2

  ...                   ...                 ...                ...

ABC             10101            0.00           1000

 

If I do a simple merge by Permno, I only get the match for one Fund, while the other disappear.

I would like to merge by Permno (only shared variable), but by having every fund treated as an independent observations in the merging:

Desired merged dataset

Fund         Target         Permno        Return         Date

Fund_A       ABC            10101             0.01             1

   ...               ...                 ...                   ...               ...

Fund_A       ABC            10101             0.00          1000

Fund_B       ABC            10101             0.01             1

   ...               ...                 ...                   ...               ...

Fund_B       ABC            10101             0.00          1000

Fund_C       ABC            10101             0.01             1

   ...               ...                 ...                   ...               ...

Fund_C       ABC            10101             0.00          1000

 

Hopefully my explanations are clear. The answer may be easy but my limited vocabulary about SAS limits my "googling" abilities.

Thank you,

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

I think this is what you want. I inserted 5 observations for each fund instead of 1000 for demonstration purposes 🙂

 

data one;
input Fund$ Target$ Permno$;
datalines;
Fund_A ABC 10101
Fund_B ABC 10101
Fund_C ABC 10101
;

data two;
input Target$ Permno$ Return Date;
datalines;
ABC 10101 0.01 1
ABC 10101 0.02 2
ABC 10101 0.02 3
ABC 10101 0.02 4
ABC 10101 0.02 5
;

proc sql;
	create table want as
	select one.*
	      ,two.return
		  ,two.date
	from one, two;
quit;

proc print data=want;
run;

View solution in original post

4 REPLIES 4
RM6
Obsidian | Level 7 RM6
Obsidian | Level 7
try using merge by target permno
PeterClemmensen
Tourmaline | Level 20

I think this is what you want. I inserted 5 observations for each fund instead of 1000 for demonstration purposes 🙂

 

data one;
input Fund$ Target$ Permno$;
datalines;
Fund_A ABC 10101
Fund_B ABC 10101
Fund_C ABC 10101
;

data two;
input Target$ Permno$ Return Date;
datalines;
ABC 10101 0.01 1
ABC 10101 0.02 2
ABC 10101 0.02 3
ABC 10101 0.02 4
ABC 10101 0.02 5
;

proc sql;
	create table want as
	select one.*
	      ,two.return
		  ,two.date
	from one, two;
quit;

proc print data=want;
run;
Astounding
PROC Star

The solution from @PeterClemmensen looks about right.  In real life, you may have many values of PERMNO in the data set.  If that's the case, you would have to add a WHERE clause to the CREATE statement:

 

where one.permo = two.permno;

Fcot
Calcite | Level 5

Thank you very much for your help! It works

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1588 views
  • 0 likes
  • 4 in conversation