Sample 24570: Use arrays to read data and restructure a data set
The sample code on the Full Code tab illustrates how to use array processing to read external data, create new variables, and manipulate existing variables on a SAS data set.
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.
/* Read external data into three weight and date variables */
data weight;
array weigh(*) weigh1-weigh3;
array dateof(*) $ date1-date3;
input gender $ dateof(*) weigh(*);
datalines;
M 08/24/00 09/01/00 09/08/00 185 183 179
F 08/24/00 09/01/00 09/08/00 130 125 120
;
/* Use an array to group the 3 existing weight variables to */
/* calculate the difference in weight from each weigh in. Use */
/* an array to create 3 new variables to hold the calculated */
/* weight loss. */
data weight(drop=i);
set weight;
array weigh(*) weigh1-weigh3;
array diff(*) loss1-loss3 (0);
do i= 2 to dim(diff);
diff(i)=weigh(i) - weigh(i-1);
end;
run;
/* Group the weight, date, and loss variables into arrays so the data set */
/* can be restructured. New variables are created to hold values of the */
/* arrays and a new observation is created for each date. */
data weight(keep=gender dateofweigh weight lost);
set weight;
array weigh(*) weigh1-weigh3;
array dateof(*) $ date1-date3;
array loss(*) loss1-loss3;
do i= 1 to dim(weigh);
weight=weigh(i);
dateofweigh=dateof(i);
lost=loss(i);
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 gender weight dateofweigh lost
1 M 185 08/24/00 0
2 M 183 09/01/00 -2
3 M 179 09/08/00 -4
4 F 130 08/24/00 0
5 F 125 09/01/00 -5
6 F 120 09/08/00 -5
Use array processing to read external data, create new variables, and manipulate existing variables on a SAS data set.
Type: | Sample |
Topic: | SAS Reference ==> DATA Step Data Management ==> Manipulation and Transformation ==> Array processing
|
Date Modified: | 2005-12-08 11:34:05 |
Date Created: | 2004-09-30 14:08:55 |
Operating System and Release Information
SAS System | Base SAS | All | n/a | n/a |