Solved
New Contributor
Posts: 4

# array help

I need some help creating an array. This is what I have so far but I need to use an array instead of those 12 if-then statements near the end:

data combined;

MERGE       person2

person3

person4

person5

person6

person7;

by State;

Max_Income=Max(_2_person_families, _3_person_families, _4_person_families, _5_person_families, _6_person_families, _7_or_more_person_families);

Min_Income=Min(_2_person_families, _3_person_families, _4_person_families, _5_person_families, _6_person_families, _7_or_more_person_families);

If Max_Income=_2_person_families then Max_Type=2;

If Max_Income=_3_person_families then Max_Type=3;

If Max_Income=_4_person_families then Max_Type=4;

If Max_Income=_5_person_families then Max_Type=5;

If Max_Income=_6_person_families then Max_Type=6;

If Max_Income=_7_or_more_person_families then Max_Type=7;

If Min_Income=_2_person_families then Min_Type=2;

If Min_Income=_3_person_families then Min_Type=3;

If Min_Income=_4_person_families then Min_Type=4;

If Min_Income=_5_person_families then Min_Type=5;

If Min_Income=_6_person_families then Min_Type=6;

If Min_Income=_7_or_more_person_families then Min_Type=7;

proc print data=combined;

run;

Accepted Solutions
Solution
‎10-26-2015 01:50 PM
Super User
Posts: 23,675

## Re: array help

This is one of those cases where if you'd used a naming convention with a prefix instead of a suffix it would make life easier.

``````data combined;
MERGE       person2
person3
person4
person5
person6
person7;
by State;

/*You'll have to list all the variables in your array statement*/
array pf(7) _2_person_families .. _7_or_more_person_families;

max_income=max(of pf(*));
min_income=min(of pf(*));

max_type=whichn(max_income, of pf(*));
min_type=whicn(min_income, of pf(*));

run;``````

All Replies
Super User
Posts: 13,507

## Re: array help

WHICHN likely works better:

Maxtype = whichn(Max_Income, _2_person_families, _3_person_families, _4_person_families, _5_person_families, _6_person_families, _7_or_more_person_families) +1 ;

WHICHN finds which value the first argument appears in the following list by order. The +1 is so when it finds the value in _2_ the function would returen 1 and since you want a code of 2 ...

Similar for the MIn_type.

Posts: 1,147

## Re: array help

``````data combined;

MERGE person2
person3
person4
person5
person6
person7;

by State;

Max_Income=Max(_2_person_families, _3_person_families, _4_person_families, _5_person_families, _6_person_families, _7_or_more_person_families);

Min_Income=Min(_2_person_families, _3_person_families, _4_person_families, _5_person_families, _6_person_families, _7_or_more_person_families);

array ma(6) _2_person_families-_7_person_families;

do i = 2 to 7;

If Max_Income=ma(i) then Max_Type=i;
If Max_Income=ma(i) then Min_Type=i;
end;
run;

proc print data=combined;

run;``````

This is an untested code.

Thanks,

Jag

Thanks,
Jag
Solution
‎10-26-2015 01:50 PM
Super User
Posts: 23,675

## Re: array help

This is one of those cases where if you'd used a naming convention with a prefix instead of a suffix it would make life easier.

``````data combined;
MERGE       person2
person3
person4
person5
person6
person7;
by State;

/*You'll have to list all the variables in your array statement*/
array pf(7) _2_person_families .. _7_or_more_person_families;

max_income=max(of pf(*));
min_income=min(of pf(*));

max_type=whichn(max_income, of pf(*));
min_type=whicn(min_income, of pf(*));

run;``````
🔒 This topic is solved and locked.