Sample 24794: Expand single observations into multiple observations
Expand single observations with multiple variables into multiple observations with a single variable. Click on FULL CODE tab to see sample data and sample code.
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
/**********************************************************************/
/* In the following example each observation contains 1 week's worth */
/* of hours spent on a project. They are turned into one observation */
/* for each day with the hours for that day, as well as the date the */
/* hours were worked. */
/**********************************************************************/
/* Sample data set TIMES */
data times;
input date :date9. day_1 day_2 day_3 day_4 day_5 day_6 day_7;
format date date9.;
datalines;
03APR2000 4.00 9.75 9.25 4.00 9.75 5.75 0
10APR2000 8.75 7.75 0.00 0.50 2.25 9.75 0
17APR2000 4.25 6.50 8.25 0.00 8.00 2.00 0
24APR2000 3.25 6.50 8.00 8.75 4.00 9.50 0
;
data new ;
set times;
/* Group the days into an array */
array day_(*) day_1-day_7;
/* Move the information into a single variable, write the observation */
/* to the output data set and increment the DATE variable. */
do i = 1 to dim(day_);
hours=day_(i);
output;
date+1;
end;
/* Choose the variables to keep in the output data set */
keep date hours;
run;
proc print;
run;
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
OBS DATE HOURS
1 03APR2000 4.00
2 04APR2000 9.75
3 05APR2000 9.25
4 06APR2000 4.00
5 07APR2000 9.75
6 08APR2000 5.75
7 09APR2000 0.00
8 10APR2000 8.75
9 11APR2000 7.75
10 12APR2000 0.00
11 13APR2000 0.50
12 14APR2000 2.25
13 15APR2000 9.75
14 16APR2000 0.00
15 17APR2000 4.25
16 18APR2000 6.50
17 19APR2000 8.25
18 20APR2000 0.00
19 21APR2000 8.00
20 22APR2000 2.00
21 23APR2000 0.00
22 24APR2000 3.25
23 25APR2000 6.50
24 26APR2000 8.00
25 27APR2000 8.75
26 28APR2000 4.00
27 29APR2000 9.50
28 30APR2000 0.00
Expand single observations with multiple variables
into multiple observations with a single new variable.
Type: | Sample |
Topic: | SAS Reference ==> DATA Step Data Management ==> Manipulation and Transformation ==> Array processing
|
Date Modified: | 2005-12-08 11:34:35 |
Date Created: | 2004-09-30 14:09:15 |
Operating System and Release Information
SAS System | Base SAS | All | n/a | n/a |