Presentation is loading. Please wait.

Presentation is loading. Please wait.

Proc Transpose A Simple Tutorial By Charles Patridge The Hartford 860-547-6644

Similar presentations


Presentation on theme: "Proc Transpose A Simple Tutorial By Charles Patridge The Hartford 860-547-6644"— Presentation transcript:

1 Proc Transpose A Simple Tutorial By Charles Patridge The Hartford

2 A Simple Transposition The Input Data Set 1 OBS # Tester1 Tester2 Tester3 Tester The Output Data Set 2 _NAME_ COL1 COL2 COL3 COL4 COL5 COL6 COL7 COL8 Tester Tester Tester Tester Columns become Rows & Rows become Columns

3 PROC TRANSPOSE ; BY variable-1 variable-n> ; COPY variable(s); ID variable; IDLABEL variable; VAR variable(s);

4 Options DATA= input-data-set names the SAS data set to transpose. Default: most recently created SAS data set LABEL= label specifies a name for the variable in the output data set that contains the label of the variable that is being transposed to create the current observation. Default: _LABEL_ LET allows duplicate values of an ID variable. PROC TRANSPOSE transposes the observation containing the last occurrence of a particular ID value within the data set or BY group. NAME= name specifies the name for the variable in the output data set that contains the name of the variable being transposed to create the current observation. Default: _NAME_

5 Options - continued OUT= output-data-set names the output data set. If output-data-set does not exist, PROC TRANSPOSE creates it using the DATAn naming convention. Default: DATAn PREFIX= prefix specifies a prefix to use in constructing names for transposed variables in the output data set. For example, if PREFIX=VAR, the names of the variables are VAR1, VAR2,...,VARn. Interaction: when you use PREFIX= with an ID statement, the value prefixes to the ID value. Note: If a BY group in the input data set has more observations than other BY groups, PROC TRANSPOSE assigns missing values in the output data set to the variables that have no corresponding input observations.

6 Proc Transpose A simple example proc format; value mymths 1="JAN" 2="FEB" 3="MAR" 4="APR" 5="MAY" 6="JUN” 7="JUL" 8="AUG" 9="SEP" 10="OCT" 11="NOV" 12="DEC"; run; Data rawdata; infile cards missover; gender date date9. amount; year = year(date); month = month(date); cards; F 01Feb M 01Mar F 01Mar M 01Apr ;;;; run;

7 Proc Transpose – A simple example - continued proc sort data=rawdata out=rawdata; by gender year; run; proc transpose data=rawdata out=tranpose (drop=_name_); by gender year; id month ; format month mymths3.; var amount; run; proc print; run;

8 Proc Transpose A simple example Obs gender year FEB MAR APR 1 F M F 01Feb RAWDATA M 01Mar F 01Mar M 01Apr

9 Proc Transpose A simple example - continued proc transpose data=tranpose out=rawout; by gender year; var feb mar apr; run; data rawout; set rawout; tmpdate = '01'||trim(_name_)||put(year,4.); date = input(tmpdate,date9.); amount = col1; if amount =. then amount = 0; drop tmpdate _name_ col1; format date date9.; run; proc print data=rawout; run;

10 Proc Transpose A simple example Obs gender year date amount 1 F FEB F MAR F APR M FEB M MAR M APR Proc Transpose Can create records for missing OBS BUT only inclusive of existing end points!

11 Proc Transpose More Uses /*** What if you need to have a full matrix of Data ***/ data template (keep=gender date amount year); length gender $ 1.; format date date9.; tmpdate = '01dec2002'd; amount = 0; do g = 1 to 2; do d = 1 to 12; if g = 1 then gender = 'F'; if g = 2 then gender = 'M'; date = intnx('month', tmpdate, d ); output; end; run; continued….

12 proc sort data=rawdata out=rawdata ; by gender date; run; proc sort data=template out=template; by gender date; run; /*** merge template with raw data to create a full matrix of data ***/ data rawdataa; merge template (in=t) rawdata (in=r); by gender date; year = year(date); month= month(date); run; continued….

13 /*** now you would like to take vertical data to make horizontal data ***/ proc transpose data=rawdataa out=tranpose; by gender year; id month ; format month mymths3.; var amount; run; /*** print to see what data now looks like ***/ proc print data=tranpose; run;

14 Obs gender year _NAME_ JAN FEB MAR APR MAY JUN JUL AUG SEP ……. 1 F 2003 amount …. 2 M 2003 amount …. You can see we have created buckets for all the months of the year – JAN thru DEC Now, let’s do some simple math for percentages, ytd #’s and ytd %’s.

15 Continued from prior data step data rawdatab; set tranpose; array mth(*) jan feb mar apr may jun jul aug sep oct nov dec ; array pct(*) janpct febpct marpct aprpct maypct junpct julpct augpct seppct octpct novpct decpct; array ytd(*) janytd febytd marytd aprytd mayytd junytd julytd augytd sepytd octytd novytd decytd; array ytdp(*) janytp febytp marytp aprytp mayytp junytp julytp augytp sepytp octytp novytp decytp; Continued…

16 /*** get yearly totals ***/ total = sum(of jan--dec); /*** get monthly percentages to yearly totals***/ do i = 1 to 12; pct(i) = mth(i) / total; end; /*** get ytd totals ***/ do i = 1 to 12; if i = 1 then ytd(i) = mth(i); else ytd(i) = mth(i) + ytd(i-1); end; /*** get ytd percentages ***/ do i = 1 to 12; ytdp(i) = ytd(i) / total; end; drop i; run; continued…

17 Continued … /*** convert horizontal data to vertical data ***/ proc transpose data=rawdatab out=tranposeb; by gender year; var _numeric_; run; proc print data=tranposeb; run;

18 Obs gender year _NAME_ amt 1 F 2003 year F 2003 JAN F 2003 FEB F 2003 MAR F 2003 APR F 2003 MAY F 2003 JUN F 2003 JUL F 2003 AUG F 2003 SEP F 2003 OCT F 2003 NOV F 2003 DEC 0.00 Obs gender year _NAME_ amt 14 F 2003 janpct F 2003 febpct F 2003 marpct F 2003 aprpct F 2003 maypct F 2003 junpct F 2003 julpct F 2003 augpct F 2003 seppct F 2003 octpct F 2003 novpct F 2003 decpct 0.00 continued…. Continued … output

19 Obs gender year _NAME_ amt 26 F 2003 janytd F 2003 febytd F 2003 marytd F 2003 aprytd F 2003 mayytd F 2003 junytd F 2003 julytd F 2003 augytd F 2003 sepytd F 2003 octytd F 2003 novytd F 2003 decytd Obs gender year _NAME_ amt 38 F 2003 janytp F 2003 febytp F 2003 marytp F 2003 aprytp F 2003 mayytp F 2003 junytp F 2003 julytp F 2003 augytp F 2003 sepytp F 2003 octytp F 2003 novytp F 2003 decytp F 2003 total

20 Let’s Process this Last Transposed Dataset data rawdatac; set tranposeb; length month $ 3. ; _name_ = upcase(_name_); /*** make contents upper case ***/ if _name_ = "YEAR" then delete; /*** do not need this ***/ if _name_ = "TOTAL" then delete; /*** do not need this ***/ month = substr(_name_,1,3); /*** Get Name of Month ***/ if ' ' = substr(_name_,4,3) then mthly = amt; /*** get monthly amts ***/ if 'YTD' = substr(_name_,4,3) then YTD = amt; /*** get YTD amts ***/ if 'PCT' = substr(_name_,4,3) then PCT = amt; /*** get PCT amts ***/ if 'YTP' = substr(_name_,4,3) then YTDP = amt; /*** get YTD PCT amts ***/ /*** convert date field to sas date field ***/ tmpdate = '01' || month || put(year,4.); date = input( tmpdate, date9.); run; continued….

21 Continued /*** summarize over the new variables created ***/ proc summary data=rawdatac nway missing; classes gender date; var mthly pct ytd ytdp; output out=summary (drop=_type_ _freq_) sum=; run; proc print; format date date7.; run;

22 Final Output Obs gender date mthly PCT YTD YTDP 1 F 01JAN F 01FEB F 01MAR F 01APR F 01MAY F 01JUN F 01JUL F 01AUG F 01SEP F 01OCT F 01NOV F 01DEC M 01JAN M 01FEB M 01MAR …

23 Proc Transpose A Simple Tutorial The End By Charles Patridge The Hartford


Download ppt "Proc Transpose A Simple Tutorial By Charles Patridge The Hartford 860-547-6644"

Similar presentations


Ads by Google