0% found this document useful (0 votes)
30 views2 pages

SAS Programming by Example (7) : Chapter 7 Sas Arrays Avoiding Repetitious DATA Step Coding

This document contains examples of using array statements in SAS to avoid repetitive coding when working with groups of variables. The examples demonstrate substituting values, restructuring datasets to create multiple observations from single observations using arrays, and working with numeric, character and multidimensional arrays. Array statements are used with DO loops to iterate through array elements and perform the same operation on multiple variables.

Uploaded by

mihirhota
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views2 pages

SAS Programming by Example (7) : Chapter 7 Sas Arrays Avoiding Repetitious DATA Step Coding

This document contains examples of using array statements in SAS to avoid repetitive coding when working with groups of variables. The examples demonstrate substituting values, restructuring datasets to create multiple observations from single observations using arrays, and working with numeric, character and multidimensional arrays. Array statements are used with DO loops to iterate through array elements and perform the same operation on multiple variables.

Uploaded by

mihirhota
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

SAS Programming by Example [7]

Chapter 7 SAS Arrays Avoiding Repetitious DATA Step Coding


Example 1 Substituting One Value for Another in a Group of Variables Features: ARRAY Statement, DO Loop, DIM Function DATA EASYWAY; SET OLD; ARRAY NARNIA [105] X1-X100 A B C D E; DO I=1 TO 105; IF NARNIA [I] =999 THEN NARIA [1] =; END; DROP I; RUN; Example 2 Substituting One Value for Another in All Numeric Variables Features: ARRAY Statement, DO Loop, DIM Function, _NUMERIC_ Variable DATA NEW; SET OLD; ARRAY XXX [*] _NUMERIC_; DO I=1 TO DIM (XXX); IF XXX [I] =999 THEN XXX [I] =; END; DROP I; RUN; Example 3 Substituting One Value for Another in All Character Variables Features: Character Array, DO Loop, _CHARACTER_ Variable DATA NEW; SET OLD; ARRAY YYY [*] _CHARACTER_; DO I=1 TO DIM (YYY); IF YYY [I] ='NA' THEN YYY [I] =' '; END; DROP I; RUN; Note: Specify length in an array statement ARRAY X [10] 4 X1-X10; ARRAY Y [10] $1 Y1-Y10;

Example 4 Restructuring a SAS Data Set: Creating Multiple Observations From a Single Observation Features: ARRAY Statement, DO Loop, OUTPUT Statement, Restructuring a SAS Data Set Data set OLD SUBJECT X1 X2 X3 1 456 1 2 789 1 1 2 2 2 Data set NEW SUBJECT TIME X 1 4 2 5 3 6 1 7 2 8 3 9

DATA NEW; SET OLD; ARRAY XX [3] X1-X3; DO TIME=1 TO 3; X=XX [TIME]; OUTPUT; END; DROP X1-X3; RUN; Example 5 Restructuring a SAS Data Set: Creating Multiple Observations From a Single Observation (Multidimensional Example) Features: Multidimensional ARRAY Statement, Nested DO Loops Data set OLD Data set NEW SUBJECT X1 X2 X3 X4 X5 X6 SUBJECT METHOD TIME X 1 456789 1 1 1 4 2 789123 1 1 2 5 2 2 3 3 DATA NEW; SET OLD; ARRAY XX [2, 3] X1-X6; DO METHOD=1 TO 2; DO TIME=1 TO 3; X=XX [METHOD, TIME]; OUTPUT; END; END; KEEP SUBJECT METHOD TIME X; RUN;

You might also like