STAT6250ArraysDr. Fan

Reading assignment: Chapter 13

Basic Arrays

Syntax:

ARRAY name_of_array{n} var1 var2 … varn;

  • ARRAY creates an array of n variables; each variable is an element in the array
  • Array names follow the same rules as those for variable names
  • The elements of an array must be all numeric or all character— no mix allowed
  • It is a good habit to include $ and size after the array name in every character array
  • When array size n is unknown, replace it by *
  • _CHARACTER_ represents all previous defined character variables
  • _NUMERIC_ represents all previous defined numeric variables

Example:

*Program 13-1 Converting values of 999 to a SAS missing

value - without using arrays;

data new;

set learn.SPSS;

if Height = 999then Height = .;

if Weight = 999then Weight = .;

if Age = 999then Age = .;

run;

*Program 13-2 Converting values of 999 to a SAS missing value

- using arrays;

data new;

set learn.SPSS;

array myvars{3} Height Weight Age;

do i = 1to3;

if myvars{i} = 999then myvars{i} = .;

end;

drop i;

run;

*Program 13-4 Converting values of NA and ? to a character missing values;

data learn.chars;

input A $ B $ x y Ques $;

datalines;

NA ? 3 4 ABC

AAA BBB 8 . ?

NA NA 9 8 NA

;

data missing;

set learn.chars;

array char_vars{*} $ _character_;

do loop = 1to dim(char_vars);

if char_vars{loop} in ('NA''?') then

char_vars{loop}=' ';

end;

drop loop;

run;

Specify the Array Bounds

Syntax:

ARRAY name_of_array{lower:upper} varlower varlower+1 … varupper;

Example:

*Program 13-7 Changing the array bounds;

data account;

input ID Income1999-Income2006;

array income{1999:2006} Income1999-Income2006;

array taxes{1999:2006} Taxes1999-Taxes2006;

do Year = 1999to2006;

Taxes{Year} = .25*Income{Year};

end;

drop Year;

format Income1999-Income2006

Taxes1999-Taxes2006 dollar10.;

datalines;

001 45000 47000 47500 48000 48000 52000 53000 55000

002 67130 68000 72000 70000 65000 52000 49000 40100

;

Temporary Arrays

Syntax:

ARRAY name_of_array{n} _TEMPORARY_ (elm1, elm2, … ,elmn);

Example:

*Program 13-8 Using a temporary array to score a test;

data score;

array ans{10} $ 1;

array key{10} $ 1_temporary_

('A','B','C','D','E','E','D','C','B','A');

input ID (Ans1-Ans10)($1.);

RawScore = 0;

do Ques = 1to10;

RawScore + (key{Ques} eq Ans{Ques});

end;

Percent = 100*RawScore/10;

keep ID RawScore Percent;

datalines;

123 ABCDEDDDCA

126 ABCDEEDCBA

129 DBCBCEDDEB

;

*Program 13-9 Loading the initial values of a temporary array

from a raw data file;

data score;

array ans{10} $ 1;

array key{10} $ 1_temporary_;

/* Load the temporary array elements */

if _n_ = 1thendo Ques = 1to10;

input key{Ques} $1. @;

end;

input ID (Ans1-Ans10)($1.);

datalines;

ABCDEEDCBA

123 ABCDEDDDCA

126 ABCDEEDCBA

129 DBCBCEDDEB

;

Multidimensional Arrays

Syntax:

ARRAY name_of_array{m, n} var1,1 var1,2 … var1,n …varm,1 varm,2 … varm,n

  • It creates a m by n array and reads the initial values row by row

Example:See Page 255

*Program 13-10 Loading a two-dimensional, temporary array with data values;

data look_up;

array level{1944:1949,5} _temporary_;

if _n_ = 1thendo Year = 1944to1949;

do Job = 1to5;

input level{Year,Job} @;

end;

end;

set learn.expose;

/* Compute the job code index from the JobCode value */

Job = input(translate(Jobcode,'12345','ABCDE'),1.);

Benzene = level{Year,Job};

drop Job;

datalines;

220 180 210 110 90

202 170 208 100 85

150 110 150 60 50

105 56 88 40 30

60 30 40 20 10

45 22 22 10 8

;

1