Sample 24745: Collapsing observations within a BY-Group into a single observation
Rearrange a data set by changing a single variable in a group of observations to a group of variables in one observation. Reshape data by collapsing observations within a BY-Group into a single observation in order to simplify data analysis and report generation.
Note:
This is Example 5.14 from
Combining and Modifying SAS Data Sets:Examples.
/* Create sample data */
data students;
input name:$ score;
datalines;
Deborah 89
Deborah 90
Deborah 95
Martin 90
Stefan 89
Stefan 76
;
data scores(keep=name score1-score3);
/* RETAIN prevents these variables from being set to missing at the top of */
/* the DATA step with each iteration */
retain name score1-score3;
/* An ARRAY statement is used to name the new variables */
array scores(*) score1-score3;
set students;
by name;
if first.name then do;
i=1;
/* Clear the array so that values from the last BY-Group aren't carried forward */
do j=1 to 3;
scores(j)=.;
end;
end;
/* Assign the values to the array elements */
scores(i)=score;
if last.name then output;
i+1;
run;
proc print;
run;
/* Alternate method using PROC TRANSPOSE to yield the same result */
proc transpose data=students out=new(drop=_name_) prefix=score;
by name;
var score;
run;
Obs name score1 score2 score3
1 Deborah 89 90 95
2 Martin 90 . .
3 Stefan 89 76 .
Rearrange a data set by changing a single variable in a group of observations to a group of variables in one observation.
| Type: | Sample |
| Topic: | SAS Reference ==> DATA Step Data Management ==> Manipulation and Transformation ==> Array processing Data Management ==> Manipulation and Transformation ==> BY-group processing
|
| Date Modified: | 2006-04-15 03:02:59 |
| Date Created: | 2004-09-30 14:09:10 |
Operating System and Release Information
| SAS System | Base SAS | All | n/a | n/a |