Help using Base SAS procedures

about merging by date

Reply
Occasional Contributor bqk
Occasional Contributor
Posts: 16

about merging by date

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
Super User
Posts: 9,681

Re: about merging by date

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
Ask a Question
Discussion stats
  • 1 reply
  • 105 views
  • 0 likes
  • 2 in conversation