Help using Base SAS procedures

How can I use like ' ' to choose sth start with letter but followed only numeric numbers

Accepted Solution Solved
Reply
Contributor
Posts: 28
Accepted Solution

How can I use like ' ' to choose sth start with letter but followed only numeric numbers

Hello,

 

For a variable called Marketing_Source_Code, I want to exclude the code starting with a “P” followed by 3 digits. for example 'P121'.

I've used where Marketing_Source_Code like 'P___', but it will also delete the ones start with "P' followered by characters, for example 'PERN'.

 

How can I only exclude the code starting with a “P” followed by 3 digits.  I may need this in urgent but dont know how to search the result.

 

Thanks a lot.


Accepted Solutions
Solution
‎01-17-2017 02:15 PM
Contributor
Posts: 28

Re: How can I use like ' ' to choose sth start with letter but followed only numeric numbers

proc sql;

delete from plc_cust_income

where (MKT_SOURCE_CODE='RCS1' OR MKT_SOURCE_CODE='BFCE' OR MKT_SOURCE_CODE='EXEM' OR (MKT_SOURCE_CODE like "P%" and anyalpha(substr(MKT_SOURCE_CODE,2,3))=0)) Smiley Wink

 

Is that right?

thanks

View solution in original post


All Replies
Trusted Advisor
Posts: 1,399

Re: How can I use like ' ' to choose sth start with letter but followed only numeric numbers

You can do:

 

if length(strip(translate(var,' ','0123456789')))=1 then put 'OK';  else put 'NOT-OK';

SAS Employee
Posts: 8

Re: How can I use like ' ' to choose sth start with letter but followed only numeric numbers

[ Edited ]

Hello,

 

ANYDIGIT might help in that case.

 

data b ;
set a ;
where not(a like "P%" and anydigit(a) in (2,3,4)) ;
run ;

 

---Update---

Well, it is more tricky than I thought...

This one should work better:

 

data b ;
set a ;
where not(a like "P%" and anyalpha(substr(a,2,3))=0) ;
run ;

 

 

Nicolas.

Solution
‎01-17-2017 02:15 PM
Contributor
Posts: 28

Re: How can I use like ' ' to choose sth start with letter but followed only numeric numbers

proc sql;

delete from plc_cust_income

where (MKT_SOURCE_CODE='RCS1' OR MKT_SOURCE_CODE='BFCE' OR MKT_SOURCE_CODE='EXEM' OR (MKT_SOURCE_CODE like "P%" and anyalpha(substr(MKT_SOURCE_CODE,2,3))=0)) Smiley Wink

 

Is that right?

thanks

Contributor
Posts: 28

Re: How can I use like ' ' to choose sth start with letter but followed only numeric numbers

WORKS, THANK YOU
Super User
Posts: 5,091

Re: How can I use like ' ' to choose sth start with letter but followed only numeric numbers

Some relevant questions:

 

1. Is your variable always 4 characters long?

 

2. If it is only 3 characters long in some cases, how should P12 be handled?

Contributor
Posts: 28

Re: How can I use like ' ' to choose sth start with letter but followed only numeric numbers

yes, it is 4 characters long always
Super User
Posts: 5,091

Re: How can I use like ' ' to choose sth start with letter but followed only numeric numbers

OK.  It's possible that some of the earlier solutions posted by others are working.  Here's a DATA step statement that I know works when you always have four characters:

 

if Marketing_Source_Code =: 'P' and input(substr(Marketing_Source_Code, 2), ??3.) > . then delete;

Contributor
Posts: 28

Re: How can I use like ' ' to choose sth start with letter but followed only numeric numbers

thank you
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 172 views
  • 0 likes
  • 4 in conversation