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

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisNZ
Tourmaline | Level 20

Or this :

IMAGING = prxmatch('/CT|XRAY|MRI/i',PROCS) > 0;

View solution in original post

11 REPLIES 11
Reeza
Super User
I think that's just an IF statement or possibly a format.

if upcase(procs) in ("CT", "XRAY", ......) the imaging=1; else imaging=0;
Astounding
PROC Star

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.

novinosrin
Tourmaline | Level 20
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;
hashman
Ammonite | Level 13

@novinosrin:

Funny that I hadn't seen your code before posting mine ;). 

novinosrin
Tourmaline | Level 20

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

 

hashman
Ammonite | Level 13

@novinosrin : 

p.s. Still am. Thanks for the good wishes. Off homeward tomorrow ... a long drive. 

hashman
Ammonite | Level 13

@stancemcgraw :

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.

ChrisNZ
Tourmaline | Level 20

Or this :

IMAGING = prxmatch('/CT|XRAY|MRI/i',PROCS) > 0;
hashman
Ammonite | Level 13

@ChrisNZ : That's actually the best approach I can think of, especially since this regex expression is easy to comprehend.

ChrisNZ
Tourmaline | Level 20

@hashman  Yes, a simple legible expression for once! It's almost troubling...   🙂

hashman
Ammonite | Level 13

@ChrisNZ : Extremely troubling and disturbing, indeed ...

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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
  • 11 replies
  • 1255 views
  • 12 likes
  • 6 in conversation