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

Hi Community,

 

I hoping you could help me determine how to create and compute 2 new summary variables that capture the total number of contacts based on provider type (MD or Social worker (SW)). The 2 new variables need to be continuous. As you can see in the table below, study_id 1001 received a total of 8 contacts during the follow-up period: 6 MD & 2 SW. I would to write a SAS code to create and compute the 2 variables (MD_tot and SW_tot). Please help or point me in the right direction.

Thanks so much!

 

Data set: 2250 observations

Total # variables in data set: 138

Variables related to this question: (in table below)

Variables related to this question are coded as: Con1-Con8 (1 =MD, 2 =SW; binary)

New variables I'd like to create: count of total Con1-Con8 by provider contact. These will be continuous variables

 

Sample subset of data set

study_idcon_totcon1con2con3con4con5con6con7con8MD_totSW_tot
1001811111221??
1002212      ??

 

 
 
 
 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
biopharma
Quartz | Level 8

Does this work for you?

data have ;
   input study_id con_tot con1 con2 con3 con4 con5 con6 con7 con8 ;
   cards ;
1001 8 1 1 1 1 1 2 2 1		
1002 2 1 2 . . . . . .		
;
run ;

data want ;
   set have ;
   array _con (*) con1 - con8 ;
   do _n_ = 1 to dim(_con) ;
      md_tot = sum(md_tot,(_con(_n_)=1)) ;
      sw_tot = sum(sw_tot,(_con(_n_)=2)) ;
   end ;
run ;

View solution in original post

4 REPLIES 4
biopharma
Quartz | Level 8

Does this work for you?

data have ;
   input study_id con_tot con1 con2 con3 con4 con5 con6 con7 con8 ;
   cards ;
1001 8 1 1 1 1 1 2 2 1		
1002 2 1 2 . . . . . .		
;
run ;

data want ;
   set have ;
   array _con (*) con1 - con8 ;
   do _n_ = 1 to dim(_con) ;
      md_tot = sum(md_tot,(_con(_n_)=1)) ;
      sw_tot = sum(sw_tot,(_con(_n_)=2)) ;
   end ;
run ;
ChrisNZ
Tourmaline | Level 20

Or:

data WANT;
   set HAVE ;
   MD_TOT = lengthn(compress(catt(of CON1-CON8),' .2'));
   SW_TOT = lengthn(compress(catt(of CON1-CON8),' .1'));  
run;
Kimberly09
Fluorite | Level 6
Thanks so much! This works perfectly!
Kimberly09
Fluorite | Level 6
The code works like a charm! Thanks so much for your help.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 574 views
  • 2 likes
  • 3 in conversation