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!