Merging the Transposed Data Set

Structuring the Data for a Merge

Remember that the transposed data set, Work.Ctarget2, needs to be merged with Sasuser.Monthsum by the values of Year and Month. Neither data set is currently structured correctly for the merge.
Table 16.17 SAS Data Set: Work.Ctarget2 (first five observations)
Obs
Month
Ctarget1
Ctarget2
Ctarget3
1
Year
1997
1998
1999
2
Jan
192284420
108645734
85730444
3
Feb
86376721
147656369
74168740
4
Mar
28526103
202158055
39955768
5
Apr
260386468
41160707
31265481
Table 16.18 SAS Data Set Sasuer.Monthsum (first five observations of selected variables)
Obs
SaleMon
RevCargo
MonthNo
1
JAN1997
$171,520,869.10
1
2
JAN1998
$238,786,807.60
1
3
JAN1999
$280,350,393.00
1
4
FEB1997
$177,671,530.40
2
5
FEB1998
$215,959,695.50
2

Using a BY Statement with PROC TRANSPOSE

In order to correctly structure Work.Ctarget2 for the merge, a BY statement needs to be used with PROC TRANSPOSE. For each BY group, PROC TRANSPOSE creates one observation for each variable that it transposes. The BY variable itself is not transposed.
The following program transposes Sasuser.Ctargets by the value of Year. The resulting output data set, Work.Ctarget2, now contains 12 observations for each year (1997, 1998, and 1999).
proc transpose data=sasuser.ctargets 
     out=work.ctarget2
     name=Month
     prefix=Ctarget;
     by year;
run;
Table 16.19 Input Data Set Sasuser.Ctargets (selected variables)
Obs
Year
Jan
Feb
Mar
Apr
May
Jun
1
1997
192284420
86376721
28526103
260386468
109975326
102833104
2
1998
108645734
147656369
202158055
41160707
264294440
267135485
3
1999
85730444
74168740
39955768
312654811
318149340
187270927
Table 16.20 Output Data Set Work.Ctarget2 (first 12 observations)
Obs
Year
Month
Ctarget1
1
1997
Jan
192284420
2
1997
Feb
86376721
3
1997
Mar
28526103
4
1997
Apr
260386468
5
1997
May
109975326
6
1997
Jun
102833104
7
1997
Jul
196728648
8
1997
Aug
236996122
9
1997
Sep
112413744
10
1997
Oct
125401565
11
1997
Nov
72551855
12
1997
Dec
136042505
CAUTION:
The original SAS data set must be sorted or indexed before using a BY statement with PROC TRANSPOSE unless you use the NOTSORTED option.

Sorting the Work.Ctarget2 Data Set

The last step in preparing Work.Ctarget2 for the merge is to use the SORT procedure to sort the data set by Year and Month as shown in the following program:
proc sort data=work.ctarget2;
   by year month;
run; 
Notice that in the sorted version of Work.Ctarget2, the values of month are sorted alphabetically within year.
Table 16.21 SAS Data Set Work.Ctarget2 (sorted, first 12 observations)
Obs
Year
Month
Ctarget1
1
1997
Apr
260386468
2
1997
Aug
236996122
3
1997
Dec
136042505
4
1997
Feb
86376721
5
1997
Jan
192284420
6
1997
Jul
196728648
7
1997
Jun
102833104
8
1997
Mar
28526103
9
1997
May
109975326
10
1997
Nov
72551855
11
1997
Oct
125401565
12
1997
Sep
112413744

Reorganizing the Sasuser.Monthsum Data Set

The data in Sasuser.Monthsum must also be reorganized for the merge because the month and year values in that data set are combined in the variable SaleMon.
Table 16.22 SAS Data Set Sasuser.Monthsum (first five observations of selected variables)
Obs
SaleMon
RevCargo
MonthNo
1
JAN1997
$171,520,869.10
1
2
JAN1998
$238,786,807.60
1
3
JAN1999
$280,350,393.00
1
4
FEB1997
$177,671,530.40
2
5
FEB1998
$215,959,695.50
2
The following program creates two new variables, Year and Month, to hold the year and month values. The values for Year are created from SaleMon using the INPUT and SUBSTR functions. The values for Month are extracted from SaleMon using the LOWCASE and SUBSTR functions.
data work.mnthsum2;
   set sasuser.monthsum(keep=SaleMon RevCargo);
   length Month $ 3;
   Year=input(substr(SaleMon,4),4.);
   Month=propcase(SaleMon);
 run;
Table 16.23 SAS Data Set Work.Mnthsum2 (first six observations)
Obs
SaleMon
RevCargo
Month
Year
1
JAN1997
$171,520,869.10
Jan
1997
2
JAN1998
$238,786,807.60
Jan
1998
3
JAN1999
$280,350,393.00
Jan
1999
4
FEB1997
$177,671,530.40
Feb
1997
5
FEB1998
$215,959,695.50
Feb
1998
6
FEB1999
$253,999,924.00
Feb
1999

Sorting the Work.Mnthsum2 Data Set

As with Work.Ctarget2, the last step in preparing for the merge is to sort the data set by the values of Year and Month as shown in the following program:
proc sort data=work.mnthsum2;
   by year month;
run;
Notice that in the sorted version of Work.Mnthsum2, the values of month are sorted alphabetically within year.
Table 16.24 SAS Data Set Work.Mnthsum2 (sorted, first twelve observations)
Obs
SaleMon
RevCargo
Month
Year
1
APR1997
$380,804,120.20
Apr
1997
2
AUG1997
$196,639,501.10
Aug
1997
3
DEC1997
$196,504,413.00
Dec
1997
4
FEB1997
$177,671,530.40
Feb
1997
5
JAN1997
$171,520,869.10
Jan
1997
6
JUL1997
$197,163,278.20
Jul
1997
7
JUN1997
$190,560,828.50
Jun
1997
8
MAR1997
$196,591,378.20
Mar
1997
9
MAY1997
$196,261,573.20
May
1997
10
NOV1997
$190,228,066.70
Nov
1997
11
OCT1997
$196,957,153.40
Oct
1997
12
SEP1997
$190,535,012.50
Sep
1997

Completing the Merge

When the data is structured correctly, Work.Mnthsum2 and Work.Ctarget2 can be merged by the values of Year and Month as shown in the following program:
data work.merged;
   merge work.mnthsum2 work.ctarget2;
   by year month;
run;
Table 16.25 SAS Data Set Work.Mnthsum2 (first five observations)
Obs
SaleMon
RevCargo
Month
Year
1
APR1997
$380,804,120.20
Apr
1997
2
AUG1997
$196,639,501.10
Aug
1997
3
DEC1997
$196,504,413.00
Dec
1997
4
FEB1997
$177,671,530.40
Feb
1997
5
JAN1997
$171,520,869.10
Jan
1997
Table 16.26 SAS Data Set Work.Ctarget2 (first five observations)
Obs
Year
Month
Ctarget1
1
1997
Apr
260386468
2
1997
Aug
236996122
3
1997
Dec
136042505
4
1997
Feb
86376721
5
1997
Jan
192284420
PROC PRINT output shows the resulting data set Work.Merged. The values of RevCargo represent the actual cargo revenue for each month. The values of Ctarget1 represent the target cargo values for each month.
proc print 
     data=work.merged (obs=10);
   format ctarget1 dollar15.2;
   var month year revcargo ctarget1;
run;
SAS Data Set Work
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.188.238