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.
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 ...
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 ...
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.
For the best of both worlds, combine those ideas:
data want;
set have;
if barcode in: ('4809', '4906');
run;
in: or =: will match 480 and 490 to your codes as well.
@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
;
Thanks for the check!
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.