Sample 24635: Expanding SAS® data set observations
Reshape data by creating multiple observations from a single observation in the input data set and by assigning variable names as values in the output data set.
Note: This is Example 5.15 from Combining and Modifying SAS Data Sets - Examples.
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.
/* Create sample data set */
data survey;
input name $ cereal pastry bagel;
datalines;
John 10 9 8
Sam 2 8 4
Sally 5 7 6
;
/* Use an array and DO loop to create multiple observations from each */
/* single observation in the input data set. Use CALL VNAME to assign */
/* variable names from the input data set as values of a new variable */
/* in the output data set. The numeric values of the variables */
/* CEREAL, PASTRY, and BAGEL are written to the new variable RESPONSE. */
data survey2(drop=cereal pastry bagel i);
set survey;
/* Define the array NUM. */
array num (*) cereal pastry bagel;
/* Define character variable BREAKFST and give it a length of 8. */
length breakfst $ 8;
do i=1 to dim(num);
/* Assign the values to RESPONSE and BREAKFST. Write an observation */
/* to SURVEY2 each time the DO loop iterates. */
response=num(i);
call vname(num(i),breakfst);
output;
end;
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 name breakfst response
1 John cereal 10
2 John pastry 9
3 John bagel 8
4 Sam cereal 2
5 Sam pastry 8
6 Sam bagel 4
7 Sally cereal 5
8 Sally pastry 7
9 Sally bagel 6
Read in a SAS data set and create multiple observations from each observation read.
Type: | Sample |
Topic: | SAS Reference ==> DATA Step Data Management ==> Manipulation and Transformation ==> Array processing
|
Date Modified: | 2006-06-22 03:02:57 |
Date Created: | 2004-09-30 14:08:59 |
Operating System and Release Information
SAS System | Base SAS | All | n/a | n/a |