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_id | con_tot | con1 | con2 | con3 | con4 | con5 | con6 | con7 | con8 | MD_tot | SW_tot |
| 1001 | 8 | 1 | 1 | 1 | 1 | 1 | 2 | 2 | 1 | ? | ? |
| 1002 | 2 | 1 | 2 | ? | ? |
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 ;
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 ;
Or:
data WANT; set HAVE ; MD_TOT = lengthn(compress(catt(of CON1-CON8),' .2')); SW_TOT = lengthn(compress(catt(of CON1-CON8),' .1'));
run;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.