1 / 12

Changing a file from being long to being wide*

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 ;

denton
Download Presentation

Changing a file from being long to being wide*

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  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 2010 Josh yes 1 2 3 4 19:10 19:12 2010 2010 Josh no 5 6 7 8 08:05 08:07 2010 2010 Josh yes 5 6 7 8 08:05 08:07 2010 2010 ;

  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_1 x1_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 2010 Mary no 1 2 3 4 19:10 19:12 2010 2010 5 6 7 8 08:05 08:07 2010 2010 ;

  4. A Proc Transpose Solution /* sort data */ proc sort data=have out=temp1; by First_Name status; run;

  5. A Proc Transpose Solution /* sort data */ proc sort data=have out=temp1; by First_Name status; run; /* 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;

  6. A Proc Transpose Solution /* sort data */ proc sort data=have out=temp1; by First_Name status; run; /* 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;

  7. A Proc Transpose Solution /* sort data */ proc sort data=have out=temp1; by First_Name status; run; /* 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;

  8. A Proc Transpose Solution /* sort data */ proc sort data=have out=temp1; by First_Name status; run; /* 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;

  9. A Proc Transpose Solution /* sort data */ proc sort data=have out=temp1; by First_Name status; run; /* 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;

  10. Changing a file from being long to being wide or

  11. A Proc Summary Solution proc summary data=have nway; class first_name status; output out=need (drop=_:) idgroup(out[2](x1--year2)=); run;

  12. Changing a file from being long to being wide Questions?

More Related