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 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

From SAS Users blog
Want more? Visit our blog for more articles like these.
5 Steps to Your First Analytics Project Using SAS

For SAS newbies, this video is a great way to get started. James Harroun walks through the process using SAS Studio for SAS OnDemand for Academics, but the same steps apply to any analytics project.

Find more tutorials on the SAS Users YouTube channel.

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