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?
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.

pcelbaCommented:
Yes, you should be able to write:

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

or

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

More info: http://support.sas.com/documentation/cdl/en/lrcon/62955/HTML/default/viewer.htm#a000999255.htm

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';
0
mlmccCommented:
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.

mlmcc
0
pcelbaCommented:
Good point!  This possibility should solve some input mask.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy 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.
0
mlmccCommented:
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'

mlmcc
0
pcelbaCommented:
I wrote it in the first answer already:

IF dishr < '00' OR dishr > '23' then flag = 'Invalid';
0
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 $;
  flag='';
  IF dishr < '00' or dishr > '23' then flag = 'InValid';
  else flag='Valid';
datalines;
'01'
'03'
'23'
'~1'
'1~'
'25'
'A0'
'a2'
'2t'
;
proc print data=test;
run;
============================

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?
0
pcelbaCommented:
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.

Etc.

Just open the SAS documentation and read about available functions.
0
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'
0

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.
0
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
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.