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;
user | Observation | observation1 |
8989 | Multiple SIV types were detected | Multiple SIV types |
367 | sample type indicated the presence of subtype H1. However, | presence of subtype H1 |
1427 | sample type indicated the presence of subtype H1. However, the | presence of subtype H1 |
3046 | sample type this case and indicated the presence of multiple types, including H1, H3, N1, and N2. | multiple types, including H1, H3 and N1 |
3146 | sample type and indicated the presence of multiple types, including H1, H3, N1, and N2. | multiple types, including H1, H3, N1, and N2 |
8910 | sample type (H,N) fluid indicated the presence of multiple types, including H1, H3 and N2. | multiple types, including H1, H3 and N2 |
9091 | sample type indicated the presence of type N1. | presence of type N1 |
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. |
What defines a keyword? What defines the start and the end of the string to extract?
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.
This looks like something that prxmatch could help with?
@tarheel13 It sure could if only we knew what the criteria are.
@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;
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. |
Hi,
Thank you very much.
It worked and helped me a lot!
Thanks,
Moraes
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.