BookmarkSubscribeRSS Feed
stancemcgraw
Obsidian | Level 7

I have SAS desktop 9.4 and want to transpose my multi-row variable Comorbidity long ways.  Do I need to auto populate the ID to each of comorbidity (Data Want 1) or can I skip that step and transpose from Data Have to Data Want 2

 

Data have:

Accountnum      Comorbidities

240063848            Hypertension

           .                  Dementia

           .                   Mental health

240063799           Carotid

        .                     Drug abuse

 

Data want1:

Accountnum      Comorbidities

240063848            Hypertension

 240063848             Dementia

  240063848             Mental health

  240063799               Carotid

    240063799              Drug abuse

 

Eventually: data want2:

Accountnum       Hypertension      Dementia        Mental Health      Carotid      Drug Abuse

240063848                  1                     1                         1                       0                0

 240063799                 0                     0                         0                       1                1

1 REPLY 1
AlokR
Fluorite | Level 6

Yes as per my knowledge you will have to generate values for missing account numbers.
Ff you don't populate id for each comorbidity then SAS will group all missing values as one id.
Following code should work for you:

proc format;
value zero . = '0';
run;

data Have(drop=PrevAccountnum);
set Have;
format count zero.;
retain PrevAccountnum;

if missing(Accountnum) then
Accountnum=PrevAccountnum;
else PrevAccountnum=Accountnum;

Count=1;
run;

proc sort data=Have;
by Accountnum Comorbidities;
run;

proc transpose data=have out=want(drop=_name_);
id Comorbidities;
by Accountnum;
var count;
run;

sas-innovate-white.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.

 

Early bird rate extended! Save $200 when you sign up by March 31.

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 1 reply
  • 750 views
  • 0 likes
  • 2 in conversation