Link to home
Start Free TrialLog in
Avatar of Maria Torres
Maria Torres

asked on

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.
Avatar of Pavel Celba
Pavel Celba
Flag of Czechia image

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';
Avatar of Mike McCracken
Mike McCracken

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
Good point!  This possibility should solve some input mask.
Avatar of Maria Torres

ASKER

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'

mlmcc
I wrote it in the first answer already:

IF dishr < '00' OR dishr > '23' then flag = 'Invalid';
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?
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.
ASKER CERTIFIED SOLUTION
Avatar of Maria Torres
Maria Torres

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
None of the suggestions worked due to using an older version of SAS - i.e., version 8.2.