BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Smitha9
Fluorite | Level 6

Hi,

I have a dataset,

ID      Barcode

1        48095

2       4807

3        4904

4        49068

5         4906

6          49066

 

I want the data with specific first 4digits ( 4809 and 4906) doesn't matter what the 5th digit is.

ID         Barcode

1           48095

2           49068

3           4906

4           49066

 

thank you in advance.

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

If this is a character variable, you can use =: (that's equal followed by a colon) which looks to see if a string begins with a certain value

 

if barcode=:'4809' or barcode=:'4906' then ...

 

--
Paige Miller

View solution in original post

6 REPLIES 6
PaigeMiller
Diamond | Level 26

If this is a character variable, you can use =: (that's equal followed by a colon) which looks to see if a string begins with a certain value

 

if barcode=:'4809' or barcode=:'4906' then ...

 

--
Paige Miller
Reeza
Super User

Is barcode character or numeric?

If numeric use SUBSTRN, if character use SUBSTR. 

If your numbers will always be 4 or more digits in length the colon operator can also be used.

 

if substrn(barcode, 1, 4) in ('4809', '4906') then flag=1;

@Smitha9 wrote:

Hi,

I have a dataset,

ID      Barcode

1        48095

2       4807

3        4904

4        49068

5         4906

6          49066

 

I want the data with specific first 4digits ( 4809 and 4906) doesn't matter what the 5th digit is.

ID         Barcode

1           48095

2           49068

3           4906

4           49066

 

thank you in advance.


 

Astounding
PROC Star

For the best of both worlds, combine those ideas:

data want;
   set have;
   if barcode in: ('4809', '4906');
run; 
Reeza
Super User

in: or =: will match 480 and 490 to your codes as well.

Tom
Super User Tom
Super User

@Reeza wrote:

in: or =: will match 480 and 490 to your codes as well.


Not really.  That would only happen if BARCODE was incorrectly defined as length $3 or shorter.

data test;
  input string $ ;
  match1 = string in: ('4906','4806');
  match2 = substrn(string,1,4) in ('4906','4806');
cards;
4905
4906
49061
490
;

Tom_0-1660679367674.png

 

Reeza
Super User

Thanks for the check!

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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
  • 909 views
  • 3 likes
  • 5 in conversation