## Need Two-Dimensional Array Help!

Solved
Occasional Contributor
Posts: 8

# Need Two-Dimensional Array Help!

[ Edited ]

I have patient encounter file with more than 13000 records. File structure is like this.

No  Problemtype1     problemtype1b    problemtype1c  ...  problemtype49   prblemtype49c   outcome1   outcome1b ...  outcome49c

--------------------------------------------------------------------------------------------------------------------------------------------------------------

1          aa                            bb                         cc                       aa                 bb                   solved          rejected           denied

2

...

13000

I need to count respective outcomes if problemtype is 'aa'.  I am using following sample code.

data test2;
set testdata;
array problemType [4,3] ProblemType1 - ProblemType8 ProblemType1b1-ProblemType1b4;
array final_outcome {4,3} final_outcome1-final_outcome8 final_outcome1b1-final_outcome1b4;

aa=0;

Solved = 0;
Denied=0;
Rejected=0;

do i= 1 to 4;
do j=1 to 3;
if ProblemType(i,j) = 'aa' then aa=aa+ 1;

if ProblemType (i,j) = 'aa' then
if final_outcome (i,j) = 'resolved' then Solved = Solved + 1 ;
else if final_outcome (i,j) = Denied' then Denied=Denied+ 1;
else if final_outcome (i,j) = 'Rejected' then Rejected=Rejected + 1;
end;
end;
run;

This sample code works fine.  But When I specify the following code while using it on original file having 13000 records

array problemType [13000,147]  \$;
array final_outcome {13000,147) \$ ;

SAS goes into infinte loop. It  does not produce any results.

Accepted Solutions
Solution
‎09-29-2015 03:58 PM
Posts: 5,523

## Re: Need Two-Dimensional Array Help!

Maybe your variable names are not exactly as you say and there is a name conflict... My code assumes that the list of problemType variables matches the list of outcome variables (same number and order).

Try using unrelated variable names to avoid conflict

``````data test2;
set testdata;
array pt{*} problemType:;
array o{*} outcome:;
do i = 1 to dim(pt);
cat = substr(vname(pt{i}), 12);
pType = pt{i};
outcm = o{i};
output;
end;
keep No cat pType outcm;
run;

proc sql;
create table counts as
select pType, outcm, count(*) as n
from test2
group by pType, outcm;
quit;

proc transpose data=counts;
where pType = "aa";
var n;
id outcm;
run;``````
PG

All Replies
Super Contributor
Posts: 324

## Re: Need Two-Dimensional Array Help!

You are mixing the array declaration and array usage with 3 types(like ( ), [ ], { }). Better to stick to one usage. I usually stay with [ ].

I am not sure whether my suggestion will fix your problem.

Occasional Contributor
Posts: 8

## Re: Need Two-Dimensional Array Help!

I also tried using []. It does not change output. Thanks for reply.

Super User
Posts: 23,698

## Re: Need Two-Dimensional Array Help!

A SAS data step loops over all rows automatically, you don't need to specify 13000 as the number of rows, only the number of variables of interest. SAS does not handle datasets like an array or matrix which you may be used to from other languages.
Occasional Contributor
Posts: 8

## Re: Need Two-Dimensional Array Help!

I have shown sample code which is working fine. I have used 4 rows and 3 columns. If I dont mention 13000 as row then it doesnt go in next row.

If there is no need to mention 13000 as row.How to dynamically define 2 dimensional array.

I have used single dimensional dynamic array using

array temp {*} _Character_;

I tried doing
array temp {*,*} _Character_;

Its not working.
Posts: 5,523

## Re: Need Two-Dimensional Array Help!

You can avoid a lot of trouble by first transforming your data into long form:

``````data test2;
set testdata;
array pt{*} problemType:;
array o{*} outcome:;
do i = 1 to dim(pt);
cat = substr(vname(pt{i}), 12);
problemType = pt{i};
outcome = o{i};
output;
end;
keep No cat problemType outcome;
run; ``````

then use SQL to summarize:

``````proc sql;
create table counts as
select problemType, outcome, count(*) as n
from test2
group by problemType, outcome;
quit;``````

and subset and reshape to suit your needs:

``````proc transpose data=counts;
where problemType = "aa";
var n;
id outcome;
run;``````

(untested)

PG
Occasional Contributor
Posts: 8

## Re: Need Two-Dimensional Array Help!

I used logic.I am getting error.

ERROR: Array subscript out of range at   problemType = pt{i};

Solution
‎09-29-2015 03:58 PM
Posts: 5,523

## Re: Need Two-Dimensional Array Help!

Maybe your variable names are not exactly as you say and there is a name conflict... My code assumes that the list of problemType variables matches the list of outcome variables (same number and order).

Try using unrelated variable names to avoid conflict

``````data test2;
set testdata;
array pt{*} problemType:;
array o{*} outcome:;
do i = 1 to dim(pt);
cat = substr(vname(pt{i}), 12);
pType = pt{i};
outcm = o{i};
output;
end;
keep No cat pType outcm;
run;

proc sql;
create table counts as
select pType, outcm, count(*) as n
from test2
group by pType, outcm;
quit;

proc transpose data=counts;
where pType = "aa";
var n;
id outcm;
run;``````
PG
Occasional Contributor
Posts: 8

## Re: Need Two-Dimensional Array Help!

With Minor changes, This solution worked like magic. Thanks a lot PG.
Posts: 5,523

## Re: Need Two-Dimensional Array Help!

Great! Make sure your variables lists match. Otherwise you will be pairing outcomes with the wrong problem types.

Cheers!

PG
Super User
Posts: 13,518

## Re: Need Two-Dimensional Array Help!

Proc contents output for input data set looks like ???

🔒 This topic is solved and locked.