BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Rahim_221
Calcite | Level 5
Hi,
The ID column I have in the data is a character as shown below:

ID
Ram-2020
Ram-2024
Ram-20
Ram-Sam-3032
Ram-Bak-504

My question is, I want to create a new variable named ID2 for example and that new variable contain only values when the word “Sam” is present.

So the new data set would be:

ID ID2
Ram-2020
Ram-2024
Ram-20
Ram-Sam-3032 Ram-Sam-3032
Ram-Bak-504


Thanks
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

@Rahim_221 wrote:
Thanks.
The word is alway delimited by - before and after but it may be Sam/SAM/sam.
Also, there won’t be a case when Sam is a part of another word like Samk as you asked!

What about strings that start or end with SAM?

Perhaps you want to use the FINDW() function instead. Then you can tell it what characters are delimiters between words and to ignore the case of the letters in both strings.

data want;
  set have;
  if findw(id,'sam','-','i') then id2=id;
run;

If the hyphens has to be there then add them to the search string and just use FIND() instead of FINDW().

data want;
  set have;
  if find(id,'-sam-','i') then id2=id;
run;

 

View solution in original post

6 REPLIES 6
sbxkoenk
SAS Super FREQ
data have;
LENGTH ID $ 25;
input ID $;
datalines;
Ram-2020
Ram-2024
Ram-20
Ram-Sam-3032
Ram-Bak-504
; 
run;

/* Use INDEX function */
data want;
LENGTH ID $ 25 ID2 $ 25;
 set have;
 ID2="";
 if index(ID,'Sam') then ID2=ID;
run;

proc print; run;

Koen

ballardw
Super User

You want to clearly define "word" as some functions in SAS allow to modify such definitions. Is your "word" always delimited by - before and after? Or might it be preceded by a space: "Ram Sam-3032" or spaces before and after: "Ram Sam 3032" or other characters:"Ram*Sam-3032" "Ram/Sam/3032" "Ram~Sam~3032" ? Are these matches?

What if the value is "Ram-sam-3032" or "Ram-SAM-3032" or "Ram-sAM-3032". Are these matches with different case?

What if "Sam" occurs as part of the value: "Ram-Samk-3032" is this a match?

 


@Rahim_221 wrote:
Hi,
The ID column I have in the data is a character as shown below:

ID
Ram-2020
Ram-2024
Ram-20
Ram-Sam-3032
Ram-Bak-504

My question is, I want to create a new variable named ID2 for example and that new variable contain only values when the word “Sam” is present.

So the new data set would be:

ID ID2
Ram-2020
Ram-2024
Ram-20
Ram-Sam-3032 Ram-Sam-3032
Ram-Bak-504


Thanks

 

Rahim_221
Calcite | Level 5
Thanks.
The word is alway delimited by - before and after but it may be Sam/SAM/sam.
Also, there won’t be a case when Sam is a part of another word like Samk as you asked!
sbxkoenk
SAS Super FREQ

I make it case insensitive now !

data have;
LENGTH ID $ 25;
input ID $;
datalines;
Ram-2020
Ram-2024
Ram-20
Ram-Sam-3032
Ram-Bak-504
; 
run;

/* Use INDEX function */
data want;
LENGTH ID $ 25 ID2 $ 25;
 set have;
 ID2="";
 if index(upcase(ID),'-SAM-') then ID2=ID;
run;

proc print; run;

Koen

 
Rahim_221
Calcite | Level 5
Thank you so much
Tom
Super User Tom
Super User

@Rahim_221 wrote:
Thanks.
The word is alway delimited by - before and after but it may be Sam/SAM/sam.
Also, there won’t be a case when Sam is a part of another word like Samk as you asked!

What about strings that start or end with SAM?

Perhaps you want to use the FINDW() function instead. Then you can tell it what characters are delimiters between words and to ignore the case of the letters in both strings.

data want;
  set have;
  if findw(id,'sam','-','i') then id2=id;
run;

If the hyphens has to be there then add them to the search string and just use FIND() instead of FINDW().

data want;
  set have;
  if find(id,'-sam-','i') then id2=id;
run;

 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 6 replies
  • 1098 views
  • 2 likes
  • 4 in conversation