New Contributor
Posts: 3

# Array

Let's say a company will give some coupons to their member,

the member ID is shown as follows.

there are 10 levels of membership,

For ID starting with A,B,C, the index is 1

E,F,G, the index is 2.

The numbers of coupon the member can get = 2 X index

.....

.....

A123

A124

B135

C134

D134

...

Letter     Index

A,B,C     1

D,E,F      2

G,H,I      3

J,K         4

L,M,N    5

O,P,Q    6

R,S,T     7

U,V       8

W,X      9

Y,Z       10

How can i use SAS array to do above question?

Array index[ ]

Thank you so much.

Contributor
Posts: 61

## Re: Array

I think a simple format for index will work in your case.

Frequent Contributor
Posts: 132

## Re: Array

I would agree with @ndp.  Using the format procedure will do what I believe you are looking for.  Here's some test code for you:

``````data have;
input MemberID\$;
datalines;
A123
B135
C134
D134
E134
F134
G134
H134
I134
J134
K134
L134
M134
N134
O134
P134
Q134
R134
S134
T134
U134
V134
W134
X134
Y134
Z134
;
run;

proc format;
value \$ Ind 'A'=1 'B'=1 'C'=1
'D'=2 'E'=2 'F'=2
'G'=3 'H'=3 'I'=3
'J'=4 'K'=4
'L'=5 'M'=5 'N'=5
'O'=6 'P'=6 'Q'=6
'R'=7 'S'=7 'T'=7
'U'=8 'V'=8
'W'=9 'X'=9
'Y'=10 'Z'=10;
run;

data want;
set have;
Letter=substr(MemberID,1,1);
Index=put(Letter,Ind.);
Num_Coupon=put(Index,3.)*2;
run;``````
Super User
Posts: 6,785

## Re: Array

It's not clear that arrays are the best tool for the job, but it can be done:

array letters {29} \$ 1 _temporary_ ('A' 'B' 'C' 'D' 'E' 'F' 'G' 'H' 'I' 'J' 'K' ' ' 'L' 'M' 'N' 'O' 'P' 'Q' 'R' 'S' 'T' 'U' 'V' ' ' 'W' 'X' ' ' 'Y' 'Z');

index = int( (whichc(substr(id,1,1), of letters{*}) + 2) / 3);

It's untested, so it may need a tiny bit of tweaking.

Valued Guide
Posts: 864

## Re: Array

Here is a solution but it doesn't use array:

proc format;
invalue level
'A'-'C' = 1
'D'-'F' = 2
'G'-'I' = 3
'J'-'K' = 4
'L'-'N' = 5
'O'-'Q' = 6
'R'-'T' = 7
'U'-'V' = 8
'W'-'X' = 9
'Y'-'Z' = 10
;

data have;
input Mem_Num\$;
level = substr(mem_num,1,1);
cards;
A123
A124
B135
C134
D134
;

data want;
set have;
display_level = input(level,level.);
coupon = input(level,level.) * 2;
run;

Contributor
Posts: 66

## Re: Array

Below is a relatively straightforward solution:

``````DATA have;
LENGTH member_id \$8;
INPUT member_id \$;
DATALINES;
A123
A124
B135
C134
D134
E333
®888
¥999
;
RUN;

DATA want;
SET have;
letter = SUBSTR(member_id, 1, 1);
SELECT;
WHEN (letter IN ('A','B','C')) index = 1;
WHEN (letter IN ('D','E','F')) index = 2;
WHEN (letter IN ('G','H','I')) index = 3;
WHEN (letter IN ('J','K'))     index = 4;
WHEN (letter IN ('L','M','N')) index = 5;
WHEN (letter IN ('O','P','Q')) index = 6;
WHEN (letter IN ('R','S','T')) index = 7;
WHEN (letter IN ('U','V'))     index = 8;
WHEN (letter IN ('W','X'))     index = 9;
WHEN (letter IN ('Y','Z'))     index = 10;
OTHERWISE DO;
PUT "WARNING: Bad character in letter - " letter;
END;
END;
RUN;
``````
Discussion stats
• 5 replies
• 287 views
• 1 like
• 6 in conversation