BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
BKPatel
Calcite | Level 5

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.

 

Please help me with this problem. I am new to SAS.

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

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

View solution in original post

10 REPLIES 10
KachiM
Rhodochrosite | Level 12

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.

BKPatel
Calcite | Level 5

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

Reeza
Super User
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.
BKPatel
Calcite | Level 5
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.
PGStats
Opal | Level 21

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
BKPatel
Calcite | Level 5

I used logic.I am getting error.

 

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

 

 

PGStats
Opal | Level 21

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
BKPatel
Calcite | Level 5
With Minor changes, This solution worked like magic. Thanks a lot PG.
PGStats
Opal | Level 21

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

 

Cheers!

PG
ballardw
Super User

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

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 10 replies
  • 1962 views
  • 0 likes
  • 5 in conversation