changing a file from being long to being wide*

12
Changing a file from being long to being wide* September 17, 2010 A summary of SAS-L posts by Ian Whitlock and

Upload: morrison

Post on 06-Jan-2016

16 views

Category:

Documents


0 download

DESCRIPTION

Changing a file from being long to being wide*. September 17, 2010. A summary of SAS-L posts by Ian Whitlock and datanull. The File You Have. data have; input First_Name $ status $ x1 x2 y1 y2 (hour1 hour2) (:time.) year1 year2; format hour: hhmm5.; cards ; - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Changing a file from being long to being wide*

Changing a file from

being longto

being wide*

September 17, 2010

•A summary of SAS-L posts by Ian Whitlock and datanull

Page 2: Changing a file from being long to being wide*

2

The File You Have

data have;input First_Name $ status $ x1 x2 y1 y2 (hour1 hour2) (:time.) year1 year2;format hour: hhmm5.;cards ;

Josh no 1 2 3 4 19:10 19:12 2010 2010Josh yes 1 2 3 4 19:10 19:12 2010 2010Josh no 5 6 7 8 08:05 08:07 2010 2010Josh yes 5 6 7 8 08:05 08:07 2010 2010;

Page 3: Changing a file from being long to being wide*

3

The File You Need

data need;input First_Name $ status $

x1_1 x2_1 y1_1 y2_1(hour1_1 hour2_1) (:time.) year1_1 year2_1x1_2 x2_2 y1_2 y2_2(hour1_2 hour2_2) (:time.) year1_2 year2_2;

format hour: hhmm5.;cards ;

Josh no 1 2 3 4 19:10 19:12 2010 2010 5 6 7 8 08:05 08:07 2010 2010Mary no 1 2 3 4 19:10 19:12 2010 2010 5 6 7 8 08:05 08:07 2010 2010

;

Page 4: Changing a file from being long to being wide*

4

A Proc Transpose Solution

/* sort data */proc sort data=have out=temp1; by First_Name status;run;

Page 5: Changing a file from being long to being wide*

5

/* sort data */proc sort data=have out=temp1; by First_Name status;run;

A Proc Transpose Solution

/* add sequence # to make *//* one obs per by group */data temp1; set temp1; by first_name status; if first.status then seq = 0; seq + 1;run;

Page 6: Changing a file from being long to being wide*

6

/* sort data */proc sort data=have out=temp1; by First_Name status;run;

A Proc Transpose Solution

/* add sequence # to make *//* one obs per by group */data temp1; set temp1; by first_name status; if first.status then seq = 0; seq + 1;run;

/* transpose */proc transpose data = temp1 out = temp2; by first_name status seq; var x1 x2 y1 y2 hour1 hour2 year1 year2;run;

Page 7: Changing a file from being long to being wide*

7

/* sort data */proc sort data=have out=temp1; by First_Name status;run;

A Proc Transpose Solution

/* add sequence # to make *//* one obs per by group */data temp1; set temp1; by first_name status; if first.status then seq = 0; seq + 1;run;

/* transpose */proc transpose data = temp1 out = temp2; by first_name status seq; var x1 x2 y1 y2 hour1 hour2 year1 year2;run;

/* create names for *//* final transpose */data temp2; set temp2; _name_ = cats (_name_ , "_",

seq);run;

Page 8: Changing a file from being long to being wide*

8

/* sort data */proc sort data=have out=temp1; by First_Name status;run;

A Proc Transpose Solution

/* add sequence # to make *//* one obs per by group */data temp1; set temp1; by first_name status; if first.status then seq = 0; seq + 1;run;

/* transpose */proc transpose data = temp1 out = temp2; by first_name status seq; var x1 x2 y1 y2 hour1 hour2 year1 year2;run;

/* create names for *//* final transpose */data temp2; set temp2; _name_ = cats (_name_ , "_",

seq);run;

/* produce desired file */

proc transpose data = temp2 out = need (drop=_:); by first_name status; var col1;run;

Page 9: Changing a file from being long to being wide*

9

/* sort data */proc sort data=have out=temp1; by First_Name status;run;

A Proc Transpose Solution

/* add sequence # to make *//* one obs per by group */data temp1; set temp1; by first_name status; if first.status then seq = 0; seq + 1;run;

/* transpose */proc transpose data = temp1 out = temp2; by first_name status seq; var x1 x2 y1 y2 hour1 hour2 year1 year2;run;

/* create names for *//* final transpose */data temp2; set temp2; _name_ = cats (_name_ , "_",

seq);run;

/* produce desired file */proc transpose data = temp2 out = need (drop=_:); by first_name status; var col1;run;

/* reintroduce formats */data need;

set need;format hour: hhmm5.;

run;

Page 10: Changing a file from being long to being wide*

10

Changing a file from being long to being wide

or

Page 11: Changing a file from being long to being wide*

11

A Proc Summary Solution

proc summary data=have nway; class first_name status; output out=need (drop=_:)

idgroup(out[2](x1--year2)=);run;

Page 12: Changing a file from being long to being wide*

12

Changing a file from being long to being wide

Questions?