BookmarkSubscribeRSS Feed
abdulla
Pyrite | Level 9
File A  
permnodate rtrn
1112012020.00023696170
1112012030.025770281
1112012040.00817657
2222013020.020201225
2222013030.004788088
2222013040.041050945
3332012020.016503398
3332012030.037175513
3332012040.025401699
4442013020.032370619
4442013030.037818116
4442013040.011846338
5552012030.011245678

 

File B   
datertrnHMLSMB
2012020.010.021.2
2012030.0141.43.5
2012040.0082.10.08
2013020.0233.41.69
201303-0.250.0062.14
2013040.141.213.54

 

The result I want     
permnodate rtrnrtrnHMLSMB
1112012020.000236961700.010.021.2
1112012030.0257702810.0141.43.5
1112012040.008176570.0082.10.08
2222013020.0202012250.0233.41.69
2222013030.004788088-0.250.0062.14
2222013040.0410509450.141.213.54
3332012020.0165033980.010.021.2
3332012030.0371755130.0141.43.5
3332012040.0254016990.0082.10.08
4442013020.0323706190.0233.41.69
4442013030.037818116-0.250.0062.14
4442013040.0118463380.141.213.54
5552012030.0112456780.0141.43.5

 

So, Basically I want to merge two files(A and B) together and want to fill all the cells of File B according to date. If I merge by date, I don't get the result. File B covers only first 6 rows of file A. Please help. 

7 REPLIES 7
novinosrin
Tourmaline | Level 20


data file1;
input (permno	date 	rtrn) (:$20.);
cards;
111	201202	0.00023696170
111	201203	0.025770281
111	201204	0.00817657
222	201302	0.020201225
222	201303	0.004788088
222	201304	0.041050945
333	201202	0.016503398
333	201203	0.037175513
333	201204	0.025401699
444	201302	0.032370619
444	201303	0.037818116
444	201304	0.011846338
555	201203	0.011245678
;

data file2;
input (date	rtrn	HML	SMB) ($);
cards;
201202	0.01	0.02	1.2
201203	0.014	1.4	3.5
201204	0.008	2.1	0.08
201302	0.023	3.4	1.69
201303	-0.25	0.006	2.14
201304	0.14	1.21	3.54
;

data want;
set file1;
if _n_=1 then do;
if 0 then set file2;
   dcl hash H (dataset:"file2") ;
   h.definekey  ("date") ;
   h.definedata (all:'y') ;
   h.definedone () ;
   end;
if h.find() ne 0 then call missing(rtrn,HML,	SMB);
run;

abdulla
Pyrite | Level 9
It is not working. I don't know why?
novinosrin
Tourmaline | Level 20

Well the max we/I can do is test using the samples you give us. I'm sure the code worked for your sample. So perhaps you need to be clear with your data and requirement

Astounding
PROC Star
First, figure out what to name your variables so you don't have two variables with the same name. Once that is done, sorting and merging by date gives you the right result.
Tom
Super User Tom
Super User

Merging by DATE seems to work fine.

data want;
  merge a(rename=(rtrn=rtrnA)) b(rename=(rtrn=rtrnB)) ;
  by date;
run;
proc sort;
  by permno date;
run;
Obs    permno     date       rtrnA      rtrnB     HML      SMB

  1     111      201202    0.000237     0.010    0.020    1.20
  2     111      201203    0.025770     0.014    1.400    3.50
  3     111      201204    0.008177     0.008    2.100    0.08
  4     222      201302    0.020201     0.023    3.400    1.69
  5     222      201303    0.004788    -0.250    0.006    2.14
  6     222      201304    0.041051     0.140    1.210    3.54
  7     333      201202    0.016503     0.010    0.020    1.20
  8     333      201203    0.037176     0.014    1.400    3.50
  9     333      201204    0.025402     0.008    2.100    0.08
 10     444      201302    0.032371     0.023    3.400    1.69
 11     444      201303    0.037818    -0.250    0.006    2.14
 12     444      201304    0.011846     0.140    1.210    3.54
 13     555      201203    0.011246     0.014    1.400    3.50
ChrisNZ
Tourmaline | Level 20

I don't see what the issue is. Am I missing something?

data FILE1 ;
input (PERMNO DATE RTRN) (:$20.);
cards;
111 201202 0.00023696170
111 201203 0.025770281
111 201204 0.00817657
222 201302 0.020201225
222 201303 0.004788088
222 201304 0.041050945
333 201202 0.016503398
333 201203 0.037175513
333 201204 0.025401699
444 201302 0.032370619
444 201303 0.037818116
444 201304 0.011846338
555 201203 0.011245678
run;

data FILE2;
input (DATE RTRN HML SMB) ($);
cards;
201202 0.01 0.02 1.2
201203 0.014 1.4 3.5
201204 0.008 2.1 0.08
201302 0.023 3.4 1.69
201303 -0.25 0.006 2.14
201304 0.14 1.21 3.54
run;

proc sql;
  select *
  from FILE1(rename=(RTRN=RTRNA)) 
      ,FILE2(rename=(RTRN=RTRNB)) 
  where FILE1.DATE=FILE2.DATE
  order by PERMNO, DATE;
quit;
PERMNO DATE RTRNA DATE RTRNB HML SMB
111 201202 0.00023696170 201202 0.01 0.02 1.2
111 201203 0.025770281 201203 0.014 1.4 3.5
111 201204 0.00817657 201204 0.008 2.1 0.08
222 201302 0.020201225 201302 0.023 3.4 1.69
222 201303 0.004788088 201303 -0.25 0.006 2.14
222 201304 0.041050945 201304 0.14 1.21 3.54
333 201202 0.016503398 201202 0.01 0.02 1.2
333 201203 0.037175513 201203 0.014 1.4 3.5
333 201204 0.025401699 201204 0.008 2.1 0.08
444 201302 0.032370619 201302 0.023 3.4 1.69
444 201303 0.037818116 201303 -0.25 0.006 2.14
444 201304 0.011846338 201304 0.14 1.21 3.54
555 201203 0.011245678 201203 0.014 1.4 3.5

 

VRKiwi
Obsidian | Level 7

This worked for me:

 

proc sort data=A; 
  by DATE;
run; 
 
data WANT;
  set A B; 
  merge A(rename=(RTRN=RTRNA)) B(rename=(RTRN=RTRNB)); 
  by DATE;
  keep PERMNO DATE RTRN RTRN HML SMB; 
run; 

proc sort data=WANT;
  by PERMNO DATE;
run; 

 

 

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!

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
  • 7 replies
  • 693 views
  • 0 likes
  • 6 in conversation