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

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

4 REPLIES 4
Astounding
PROC Star

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)

novinosrin
Tourmaline | Level 20
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;
novinosrin
Tourmaline | Level 20

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;
Reeza
Super User

@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. 

 

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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