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

Hello,

 

I am looking to create a new variable that will count the occurrence of a set of columns if they have characters inside of them. The dataset I am working with contains ER visits within a year of people who eventually died from X. So there are roughly 3 thousand visits and they are already sorted by a variable that ensures each patient has all there er visits in sequence. So for example if patient A went to the ER 5 times in the past year, there are 5 rows with different information but all the same patient id. I want to form a new variable that counts the number of procedures per patient. Each visit has a procedure1-procedure20 with some missing and a lot of blank. Is there any way to form a count array or something that can count each procedure and then filter it by patient, then to add up the total?

 

Thank you.

1 ACCEPTED SOLUTION

Accepted Solutions
Jagadishkatam
Amethyst | Level 16

please try the below code, please replace the number of variables , at the moment since in the sample data we have 3 variables i mentioned 3, but depending on the number of variables you can replace this 3 in array as well as cnt variable derivation. The CNT2 variable will have the actual count you are expecting. Hope this helps.

 

data have;
input Paitent_ID:   Proc1:   Proc2:   Proc3:   ;
cards;
12 2344 1233 894   
12 1902 . .
12 111 2344 9001    
12 234 . .
13 232 3934 3434
13 1444 . .
;

data want;
set have;
by Paitent_ID;
array vars(3) proc1-proc3;
cnt=3-nmiss(of proc1-proc3);
retain cnt2;
if first.Paitent_ID then cnt2=cnt;
else cnt2+cnt;
if last.Paitent_ID;
run;
Thanks,
Jag

View solution in original post

6 REPLIES 6
Jagadishkatam
Amethyst | Level 16

please provide sample data for better response.

Thanks,
Jag
dellerrrr
Fluorite | Level 6

I.E. the data is something like this

 

 

Paitent ID   Proc1   Proc2   Proc3   Proc4   Proc5...   Proc20

12               2344    1233      894      

12               1902

12               111      2344       9001    2020   

12               234

13               232      3934       3434

13               1444

 

A patient can appear from anywhere from 1 time to up to 150 times in a row

Jagadishkatam
Amethyst | Level 16

please try the below code, please replace the number of variables , at the moment since in the sample data we have 3 variables i mentioned 3, but depending on the number of variables you can replace this 3 in array as well as cnt variable derivation. The CNT2 variable will have the actual count you are expecting. Hope this helps.

 

data have;
input Paitent_ID:   Proc1:   Proc2:   Proc3:   ;
cards;
12 2344 1233 894   
12 1902 . .
12 111 2344 9001    
12 234 . .
13 232 3934 3434
13 1444 . .
;

data want;
set have;
by Paitent_ID;
array vars(3) proc1-proc3;
cnt=3-nmiss(of proc1-proc3);
retain cnt2;
if first.Paitent_ID then cnt2=cnt;
else cnt2+cnt;
if last.Paitent_ID;
run;
Thanks,
Jag
PGStats
Opal | Level 21

Change your data structure to long (patient_ID visit_ID proc_ID) and everything will get simpler to do in SAS.

PG
novinosrin
Tourmaline | Level 20
data have;
input Paitent_ID:   Proc1:   Proc2:   Proc3:   ;
cards;
12 2344 1233 894   
12 1902 . .
12 111 2344 9001    
12 234 . .
13 232 3934 3434
13 1444 . .
;

data want;
do until(last.Paitent_ID);
set have;
by  Paitent_ID;;
count=sum(count,n(of pro:));
end;
run;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 6 replies
  • 3129 views
  • 4 likes
  • 4 in conversation