BookmarkSubscribeRSS Feed
bqk
Calcite | Level 5 bqk
Calcite | Level 5
i have 2 dataset data1 data2 as following

data data1;
input date : yymmdd10. best_offer ;
format date yymmdd10.;
datalines;
2006/2/24 1.6
2006/2/24 0.3
2006/2/24 4.3
2006/2/24 1.4
2006/2/24 0.85
2006/3/20 2.5
2006/3/20 2.6
2006/3/20 1.57
2006/3/20 1.45
2006/4/19 1.78
2006/4/19 1.45
;
run;

data data2;
input futures_date : yymmdd10. futures_price ;
format futures_date yymmdd10.;
datalines;
2004/5/12 18.2
2004/5/13 19.58
2004/5/14 19.67
2004/5/17 20.21
2004/5/18 20.07
2004/5/19 19.17
2006/2/11 19.34
2006/3/15 19.17
2006/4/20 18.77
2006/5/10 17.5
2006/6/15 17.61
2006/7/20 17.35
;
run;

data data2;
input futures_date : yymmdd10. futures_price ;
format futures_date yymmdd10.;
datalines;
2004/5/12 18.2
2004/5/13 19.58
2004/5/14 19.67
2004/5/17 20.21
2004/5/18 20.07
2004/5/19 19.17
2006/2/11 19.34
2006/3/15 19.17
2006/4/20 18.77
2006/5/10 17.5
2006/6/15 17.61
2006/7/20 17.35
;
run;

i want to set the 2 dataset in the above merge as the data3

data data3;
input date : yymmdd10. best_offer futures_date : yymmdd10. futures_price ;
format date : yymmdd10. futures_date yymmdd10.;
datalines;
2006/02/24 1.6 2006/02/11 19.34
2006/02/24 0.3 2006/02/11 19.34
2006/02/24 4.3 2006/02/11 19.34
2006/02/24 1.4 2006/02/11 19.34
2006/02/24 0.85 2006/02/11 19.34
2006/03/20 2.5 2006/03/15 19.17
2006/03/20 2.6 2006/03/15 19.17
2006/03/20 1.57 2006/03/15 19.17
2006/03/20 1.45 2006/03/15 19.17
2006/04/19 1.78 2006/04/20 18.77
2006/04/19 1.45 2006/04/20 18.77
;
run;

the question is i have to merge them but they have the same year and month but the date is different.

i want to combine them with the same year and month

please help me , thx
1 REPLY 1
Ksharp
Super User
OK.That is easy.
[pre]
data data1;
input date : yymmdd10. best_offer ;
format date yymmdd10.;
flag=put(date,monyy.);
datalines;
2006/2/24 1.6
2006/2/24 0.3
2006/2/24 4.3
2006/2/24 1.4
2006/2/24 0.85
2006/3/20 2.5
2006/3/20 2.6
2006/3/20 1.57
2006/3/20 1.45
2006/4/19 1.78
2006/4/19 1.45
;
run;

data data2;
input futures_date : yymmdd10. futures_price ;
format futures_date yymmdd10.;
flag=put(futures_date,monyy.);
datalines;
2004/5/12 18.2
2004/5/13 19.58
2004/5/14 19.67
2004/5/17 20.21
2004/5/18 20.07
2004/5/19 19.17
2006/2/11 19.34
2006/3/15 19.17
2006/4/20 18.77
2006/5/10 17.5
2006/6/15 17.61
2006/7/20 17.35
;
run;
proc sort data=data1;
by flag;
run;
proc sort data=data2;
by flag;
run;
data want;
merge data1 data2;
by flag;
drop flag;
run;
proc sort data=want;
by date;
run;
[/pre]


Ksharp

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 1 reply
  • 958 views
  • 0 likes
  • 2 in conversation