Changing a file from being long to being wide*
description
Transcript of Changing a file from being long to being wide*
![Page 1: Changing a file from being long to being wide*](https://reader035.fdocuments.in/reader035/viewer/2022062517/56813d59550346895da7226e/html5/thumbnails/1.jpg)
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*](https://reader035.fdocuments.in/reader035/viewer/2022062517/56813d59550346895da7226e/html5/thumbnails/2.jpg)
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*](https://reader035.fdocuments.in/reader035/viewer/2022062517/56813d59550346895da7226e/html5/thumbnails/3.jpg)
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*](https://reader035.fdocuments.in/reader035/viewer/2022062517/56813d59550346895da7226e/html5/thumbnails/4.jpg)
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*](https://reader035.fdocuments.in/reader035/viewer/2022062517/56813d59550346895da7226e/html5/thumbnails/5.jpg)
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*](https://reader035.fdocuments.in/reader035/viewer/2022062517/56813d59550346895da7226e/html5/thumbnails/6.jpg)
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*](https://reader035.fdocuments.in/reader035/viewer/2022062517/56813d59550346895da7226e/html5/thumbnails/7.jpg)
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*](https://reader035.fdocuments.in/reader035/viewer/2022062517/56813d59550346895da7226e/html5/thumbnails/8.jpg)
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*](https://reader035.fdocuments.in/reader035/viewer/2022062517/56813d59550346895da7226e/html5/thumbnails/9.jpg)
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*](https://reader035.fdocuments.in/reader035/viewer/2022062517/56813d59550346895da7226e/html5/thumbnails/10.jpg)
10
Changing a file from being long to being wide
or
![Page 11: Changing a file from being long to being wide*](https://reader035.fdocuments.in/reader035/viewer/2022062517/56813d59550346895da7226e/html5/thumbnails/11.jpg)
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*](https://reader035.fdocuments.in/reader035/viewer/2022062517/56813d59550346895da7226e/html5/thumbnails/12.jpg)
12
Changing a file from being long to being wide
Questions?