I'm trying to create a variable that counts when a procedure says "CT", "XRAY", "MRI" and call it Imaging and then so on and so forth for other types of variable groups I want. Is there a scan or count or find function that would work in this scenario?
data have:
studynum procs
1 xray
1 leg xray
2 CT
2 head CT
3 MRI
3 Ortho
data want;
studynum procs imaging
1 xray 1
1 leg xray 1
2 CT 1
2 head CT 1
3 MRI 1
3 Ortho 0
Or this :
IMAGING = prxmatch('/CT|XRAY|MRI/i',PROCS) > 0;
You have a couple of features that make the problem mildly tricky.
The word you are searching for might not be the first word.
The case might be different (xray vs. XRAY).
Here's one way to approach that:
imaging = findw(procs, 'xray',, 'i') or findw(procs, 'mri',, 'i') or findw(procs, 'ct',, 'i');
It's untested code, so see if it works as planned.
data have;
input studynum procs & $15.;
cards;
1 xray
1 leg xray
2 CT
2 head CT
3 MRI
3 Ortho
;
data want;
set have;
do _n_=1 to countw(procs,' ');
imagining= scan(upcase(procs), _n_,' ') in ("CT", "XRAY", "MRI");
if imagining then leave;
end;
run;
Funny that I hadn't seen your code before posting mine ;).
Guru @hashman A disciple merely follows his Guru nontheless. Btw, All over Citizens , they think I'm bizzare to keep busyzaro ball rolling all the time 🙂 Part 3 and 4 are a bit of a learning curve as the concept leaps though fantastic. I a rereading. I shall touch base offline later. Take care and Good night.
PS Pass on my regards to your family and sis if you are still in philly
p.s. Still am. Thanks for the good wishes. Off homeward tomorrow ... a long drive.
In a way, you can use the combo of the COUNTW and SCAN functions to get what you want:
data have ;
input @1 studynum @4 procs $20. ;
cards ;
1 xray
1 leg xray
2 CT
2 head CT
3 MRI
3 Ortho
run ;
data want ;
set have ;
do _n_ = 1 to countw (procs) until (imaging) ;
if scan (lowcase (procs), _n_) in ("ct", "xray", "mri") then imaging = 1 ;
end ;
imaging = ^^ imaging ;
run ;
Kind regards
Paul D.
Or this :
IMAGING = prxmatch('/CT|XRAY|MRI/i',PROCS) > 0;
@ChrisNZ : That's actually the best approach I can think of, especially since this regex expression is easy to comprehend.
@hashman Yes, a simple legible expression for once! It's almost troubling... 🙂
@ChrisNZ : Extremely troubling and disturbing, indeed ...
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!
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.