How to specify a range for string characters?

I'm new to SAS and I'm trying to specify a range of string characters in an IF statement.  Is it possible to do this in SAS?  My line of code is as follows:

            IF dishr ~IN ('00'-'23') then flag = 'Invalid';

Basically, if the user enters any other values than '00', '01', '02', ..., '10', '11', '12', ... , '23', I want to set a flag that user input is invalid.  I would like to specify a range, as oppose to listing out all valid values.

Thank you for your assistance in this manner.
Maria TorresData AnalystAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Yes, you should be able to write:

IF dishr NOT BETWEEN '00' AND '23' then flag = 'Invalid';


IF NOT (dishr BETWEEN '00' AND '23') then flag = 'Invalid';

More info:

You may also look at Fully Bounded Range Condition

If nothing will work then you may use the classic expression:

IF dishr < '00' OR dishr > '23' then flag = 'Invalid';
Can the user enter 0A ?
If so then that is in that range.

If they are limited to entering only numeric characters it will work.
If they can enter anything then test for a numeric string first then the range test.

Good point!  This possibility should solve some input mask.
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Maria TorresData AnalystAuthor Commented:
I'm using SAS version 8.2.  And it does not recognize the clause "BETWEEN".  Is there some other keyword that I can use?  Thanks.
So long as you don't have an issue with non-numeric characters you should be able to do something like

dishr  >= '00' AND dishr <= '23'

I wrote it in the first answer already:

IF dishr < '00' OR dishr > '23' then flag = 'Invalid';
Maria TorresData AnalystAuthor Commented:
I incorporated the suggested line of coding and it is not working.  Below is test sample of what I coded:
data test;
  infile datalines;
  input dishr $;
  IF dishr < '00' or dishr > '23' then flag = 'InValid';
  else flag='Valid';
proc print data=test;

I need to be able detect anything that is entered that is not in the range of '00' through '03' is to be set as invalid.  So if the data has a character in the string then it is set to invalid; if the data has a numeric range that falls outside of the range '00'-'23', it is set to invalid.

I don't know why SAS is setting all observation as Invalid.  What am I doing wrong?
If your data contain alphabetic characters then you cannot use above formula as stated by mlmcc already.

You have several options...

You could use IN() function and list all valid values (if your SAS version supports it):

 IF dishr IN ('00', '01', '02',   ...    ,'23') then flag = 'Valid';

You could test the value for non-digit characters by NOTDIGIT function and then use standard IF.

You could also convert your character data into a number by INPUT() function and then simply check the numeric value range.


Just open the SAS documentation and read about available functions.
Maria TorresData AnalystAuthor Commented:
As stated in my first inquiry, I was trying to use a range so that all the list values would not have to be listed because this list will grow in the future.  Also, here in the office we are using version 8.2, so many of the functions suggested cannot be used.

So thank you for all your suggestions. I will write a routine that will parse the string and check that the string does not contain a character, as well as it falls between the range '00' to '23'

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Maria TorresData AnalystAuthor Commented:
None of the suggestions worked due to using an older version of SAS - i.e., version 8.2.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.