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

Hi all,

 

I wish to extract a specific text from each cell from the same variable.

My goal is to extract these key words from the variable (observation) to the variable observation1.

 

Thanks all in advance, 

 

So my data is described below:

 

data test2; set test2;

 

userObservationobservation1
8989Multiple SIV types were detected Multiple SIV types
367sample type      indicated the presence of subtype H1. However,presence of subtype H1
1427sample type  indicated the presence of subtype H1. However, the             presence of subtype H1
3046sample type  this case and indicated the presence of         multiple types, including H1, H3, N1, and N2.               multiple types, including H1, H3 and N1
3146sample  type and indicated the presence of    multiple types, including H1, H3, N1, and N2.       multiple types, including H1, H3, N1, and N2    
8910sample type (H,N)  fluid    indicated the presence of               multiple types, including H1, H3 and N2. multiple types, including H1, H3 and N2
9091sample type      indicated the presence of type N1. presence of type N1
1 ACCEPTED SOLUTION

Accepted Solutions
ChrisNZ
Tourmaline | Level 20

This works.

data have;
infile cards pad truncover;
input  OBSERVATION $500.;
cards;
Multiple SIV types were detected 
sample type      indicated the presence of subtype H1. However,
sample type  indicated the presence of subtype H1. However, the             
sample type  this case and indicated the presence of         multiple types, including H1, H3, N1, and N2.              
sample  type and indicated the presence of    multiple types, including H1, H3, N1, and N2.       
sample type (H,N)  fluid    indicated the presence of               multiple types, including H1, H3 and N2.
sample type      indicated the presence of type N1. 
;

data WANT;
  set HAVE;
  STR=prxchange('s/.*((multiple|presence).*\.).*/\1/',1,OBSERVATION);
  if STR=OBSERVATION then
  STR=prxchange('s/.*(multiple.*types).*/\1/i',1,OBSERVATION);
run;

OBSERVATION STR
Multiple SIV types were detected  Multiple SIV types  
sample type      indicated the presence of subtype H1. However, presence of subtype H1.
sample type  indicated the presence of subtype H1. However, the              presence of subtype H1.
sample type  this case and indicated the presence of         multiple types, including H1, H3, N1, and N2.               multiple types, including H1, H3, N1, and N2.
sample  type and indicated the presence of    multiple types, including H1, H3, N1, and N2.        multiple types, including H1, H3, N1, and N2.
sample type (H,N)  fluid    indicated the presence of               multiple types, including H1, H3 and N2. multiple types, including H1, H3 and N2.
sample type      indicated the presence of type N1.  presence of type N1.

 

View solution in original post

8 REPLIES 8
ChrisNZ
Tourmaline | Level 20

What defines a keyword? What defines the start and the end of the string to extract?

Moraes86
Obsidian | Level 7
Hi ChrisNZ,

I mean keyword for each outcome in each cell in observation1.
For example, in the second line( user 367) in this table is written: " sample type indicated the presence of subtype H1. However, presence of subtype H1"

For this variable, what defines the start and the end would be "presence of subtype H1".
My challenge is that the outcome changes for each line, as seen in the results from variable "observation1".
ballardw
Super User

Please, Please describe how you will actually use this "observation1 variable" with multiple values in it?

I see people ask for things like this often and the work to use such in anything expect a printed output is horrendous.

 

So, list the explicit values you are looking for. You are making us guess.

Since apparently you are looking at phrases have you done anything to check how consistent spelling is?

Consider this bit: "multiple types, including H1, H3 and N1"

Could it ever appear as:

"multiple types, including h1, H3 and N1"  (case of letters)

"multiple types, including H1,H3 and N1"  (change in spaces)

"multiple types,   including H1, H3 and N1" (different change in spaces)

"multiple types, including N1, H3 and H1"   (different order of words)

"multiple types including H1, H3 and N1"   (missing punctuation)

"multiple types; including H1, H3 and N1"  (different punctuation)

 

Personally if I have something similar to this I would create 5 variables,each gets a 1 when a specific one of the 5 phrases occurs and 0 if it doesn't. Then it would be easy to count how many records have which values; select records with a specific value, or combinations of values for specific tasks.

 

Awaiting a response about how consistent your data actually may be and how the variable Observation1 is to be used.

 

tarheel13
Rhodochrosite | Level 12

This looks like something that prxmatch could help with? 

singhsahab
Lapis Lazuli | Level 10

@Moraes86 I do have an alternate solution. 

 

data have;
infile cards dsd ;
input user $ Observation & $500.;
cards;
"370","sample type 	N1 indicated the presenceH3 of subtype H1 However,"
"367","sample type indicated the presence of subtype H1.H2 However,"
"368","sample type indicated the presence of subtype H1 However,H3"
"369","sample type indicated the presence of subtype H1 However,"
"3046","sample type  this case and indicated the presence of         multiple types, including H1, H3, N1, and N2.      "
;
run;

data want1;
set have;
/*Creating a variable which will contain all Finding Words*/
FINDING_WORD="H1 H2 H3 N1 N2";
COUNT=COUNTW(FINDING_WORD);
DO I=1 TO COUNT;
if INDEX(OBSERVATION,SCAN(FINDING_WORD,I)) then Flag=SCAN(FINDING_WORD,I);
OUTPUT;
END;
DROP I FINDING_WORD COUNT;
run;

PROC SORT DATA=WANT1 NODUPKEY;BY _ALL_;RUN;

DATA WANT;
SET WANT1;
BY USER Observation;
RETAIN FINDING;
IF FIRST.USER THEN FINDING=FLAG;
ELSE FINDING=CATX(',',FINDING,FLAG);
IF LAST.USER;
DROP FLAG;
RUN;

ChrisNZ
Tourmaline | Level 20

This works.

data have;
infile cards pad truncover;
input  OBSERVATION $500.;
cards;
Multiple SIV types were detected 
sample type      indicated the presence of subtype H1. However,
sample type  indicated the presence of subtype H1. However, the             
sample type  this case and indicated the presence of         multiple types, including H1, H3, N1, and N2.              
sample  type and indicated the presence of    multiple types, including H1, H3, N1, and N2.       
sample type (H,N)  fluid    indicated the presence of               multiple types, including H1, H3 and N2.
sample type      indicated the presence of type N1. 
;

data WANT;
  set HAVE;
  STR=prxchange('s/.*((multiple|presence).*\.).*/\1/',1,OBSERVATION);
  if STR=OBSERVATION then
  STR=prxchange('s/.*(multiple.*types).*/\1/i',1,OBSERVATION);
run;

OBSERVATION STR
Multiple SIV types were detected  Multiple SIV types  
sample type      indicated the presence of subtype H1. However, presence of subtype H1.
sample type  indicated the presence of subtype H1. However, the              presence of subtype H1.
sample type  this case and indicated the presence of         multiple types, including H1, H3, N1, and N2.               multiple types, including H1, H3, N1, and N2.
sample  type and indicated the presence of    multiple types, including H1, H3, N1, and N2.        multiple types, including H1, H3, N1, and N2.
sample type (H,N)  fluid    indicated the presence of               multiple types, including H1, H3 and N2. multiple types, including H1, H3 and N2.
sample type      indicated the presence of type N1.  presence of type N1.

 

Moraes86
Obsidian | Level 7

Hi,

 

Thank you very much.

It worked and helped me a lot!

 

Thanks,

 

Moraes

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
  • 8 replies
  • 1669 views
  • 7 likes
  • 5 in conversation