DATA Step, Macro, Functions and more

question about array programming

Accepted Solution Solved
Reply
Contributor
Posts: 44
Accepted Solution

question about array programming

[ Edited ]

Hello,

 

I'm really struggling to program a few arrays and would love some help!  Right now, the data looks like this:

 

Case ID     Comorbidity1   Comorbidity2  Comorbidity3

1               Diabetes            Heart failure     Lung disease

2               HTN      

3               Diabetes            Glaucoma

 

 

data want; data have;
array COMORBIDITY [3] COMORBIDITY1-COMORBIDITY3; do a=1 to 3; if COMORBIDITY [a] > ' ' then do; diagnosis = COMORBIDITY[a]; end; end;
run;
proc freq order=freq data=want; tables diagnosis; run;

 

 

I would like to know the frequency of diagnoses in the cohort of 3 people regardless of whether the diagnosis is sitting in column 1-3. 

 

For example:

Diabetes 2

HTN 1

Glaucoma 1

Heart failure 1

Lung Disease 1 

 

However, I think I'm just getting the last value...

 

My questions are:

1) Should I make a new data file called "diagnosis" and use proc transpose to make it into a long format?

2) Should I use syntax _temp_ to make a temporary long file instead of wide?

3) How would an output statement after the "end; end;" change what I get?

4) Should I use proc sql instead of the above?

 

Thanks!


Accepted Solutions
Solution
‎02-10-2018 06:15 PM
Super User
Posts: 6,629

Re: question about array programming

Good observation ... you are getting the last value only.

 

5) You should put the OUTPUT statement BEFORE the end; end; so that each nonmissing diagnosis will be output

6) There should be a SET statement (not two DATA statements)

View solution in original post


All Replies
Solution
‎02-10-2018 06:15 PM
Super User
Posts: 6,629

Re: question about array programming

Good observation ... you are getting the last value only.

 

5) You should put the OUTPUT statement BEFORE the end; end; so that each nonmissing diagnosis will be output

6) There should be a SET statement (not two DATA statements)

PROC Star
Posts: 1,570

Re: question about array programming

[ Edited ]
data have;
infile datalines truncover;
input (CaseID     Comorbidity1   Comorbidity2  Comorbidity3) (:$20.);
datalines;
1               Diabetes            Heartfailure     Lungdisease
2               HTN      . .
3               Diabetes            Glaucoma
;

data _have;
set have;
array t(*) Comorbidity:;
do _n_=1 to dim(t);
diagnosis=t(_n_);
output;
end;
run;

proc freq data=_have(where=(not missing(diagnosis)) );
tables diagnosis/out=want(drop=percent);
run;
PROC Star
Posts: 1,570

Re: question about array programming

or hashes with arrays:

 

data have;
infile datalines truncover;
input (CaseID     Comorbidity1   Comorbidity2  Comorbidity3) (:$20.);
datalines;
1               Diabetes            Heartfailure     Lungdisease
2               HTN      . .
3               Diabetes            Glaucoma
;

data _null_;
 if _N_ = 1 then do;
 	length diagnosis $20 count 8;
     declare hash h(ordered:'y' );
     h.defineKey('diagnosis');
     h.defineData('diagnosis','count');
     h.defineDone( );
     call missing(diagnosis, count);
   end;
set have end=last;
array t(*) Comorbidity:;
do _n_=1 to dim(t);
diagnosis=t(_n_);
if h.check() ne 0 then do;count=1;h.add();end;
else if h.find()=0 then do;count=count+1;h.replace();end;
end;
if last then h.output(dataset:'want(where=(not missing(diagnosis)))');
run;
Super User
Posts: 23,262

Re: question about array programming


lmyers2 wrote:

 

 

My questions are:

1) Should I make a new data file called "diagnosis" and use proc transpose to make it into a long format?

2) Should I use syntax _temp_ to make a temporary long file instead of wide?

3) How would an output statement after the "end; end;" change what I get?

4) Should I use proc sql instead of the above?

 

Thanks!


1. Yes, this is better in the long run, but you can transpose via an array or PROC TRANSPOSE

2. Not necessarily, using a long data is easier in the long run. If you want to know how many patients have X disease, you can use a single line with an IF or WHERE statement. 

3. answered by others

4. No, SQL doesn't work well with wide files either, especially because you can't use arrays or variable lists. Once its a long file, SQL is a valid option. 

 

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 175 views
  • 1 like
  • 4 in conversation