Help using Base SAS procedures

Specifying character ranges

Accepted Solution Solved
Reply
Contributor
Posts: 63
Accepted Solution

Specifying character ranges

Is there an easier way to specify a range when your variable is a character?

 

For example, I am recoding a bunch of revenue/procedure codes into categories, but using the IN() function doesn't work well for ranges that are not numeric.  So I would like to do this:

 

if c_dtl_proc_cd in (59000:59899, 90801:90899, A4651:A4932) then category='2. Other';

But this doesn't seem to work.  It seems like I would have to do this:

 

if (c_dtl_proc_cd ge '59000' and c_dtl_proc_cd le '59899') or
(c_dtl_proc_cd ge '90935' and c_dtl_proc_cd le '90899') or
(c_dtl_proc_cd ge 'A4651' and c_dtl_proc_cd le 'A4932')  then 
	category='2. Other';

 Which wouldn't be too bad, except that some of the categories have a series of different ranges.

Is there an easier way to list these ranges?

 

Thanks!


Accepted Solutions
Solution
‎10-24-2016 10:59 AM
Super User
Posts: 17,784

Re: Specifying character ranges

It's probably a bit of personal preference but I really find formats for recoding much easier to manage than multiple if then statements.

 

data format;
fmtname = "awesome_fmt";
type="C";
do value=59000 to 59899, 90935 to 90899;
start=put(value, $8.);
label='Other';
output;
end;

do value=4651 to 4932;
start=catt("A", value);
label='Other';
output;
end;

run;

proc format cntlin=format;
run;

data test;
set my_data;

category=put(c_dtl_proc_cd, $awesome_fmt.);

run;

View solution in original post


All Replies
Super User
Posts: 17,784

Re: Specifying character ranges

Not easily. Can you do a format instead? 

 

If it's character but actually a number you can convert it to numeric to check ranges?

Contributor
Posts: 63

Re: Specifying character ranges

I need to look at combinations of these categories within a visit, so I don't think a format would work well.

 

Converting it to numeric would work except that they're not all numbers, so that wouldn't work.

Super User
Posts: 17,784

Re: Specifying character ranges

In general, it appears to be a look up. You recode from one value to another. Not sure how a format wouldn't work here...

Perhaps post more sample data and expected output. Do you have your rules for recoding in a data table anywhere? Or do they exist in your head?

Contributor
Posts: 63

Re: Specifying character ranges

These are claims, so I have multiple lines for a single visit.  I think it would be much easier to create a bunch of flags to see what is happening during a visit.  I don't see how a format would work in this instance.

Solution
‎10-24-2016 10:59 AM
Super User
Posts: 17,784

Re: Specifying character ranges

It's probably a bit of personal preference but I really find formats for recoding much easier to manage than multiple if then statements.

 

data format;
fmtname = "awesome_fmt";
type="C";
do value=59000 to 59899, 90935 to 90899;
start=put(value, $8.);
label='Other';
output;
end;

do value=4651 to 4932;
start=catt("A", value);
label='Other';
output;
end;

run;

proc format cntlin=format;
run;

data test;
set my_data;

category=put(c_dtl_proc_cd, $awesome_fmt.);

run;
Contributor
Posts: 63

Re: Specifying character ranges

Yes, i agree it is sometimes personal preference, but you're right, this is probably cleaner and works just as well!  Thanks so much!

Super User
Posts: 5,081

Re: Specifying character ranges

Before you do this, you have to consider that garbage values will be included in character ranges.  For example, you have a character range:

 

"A4561" - "A4932"

 

As character strings, this range includes these values:

 

"A46.1"

"A47CD"

"A49"

 

Is your data clean enough to support a switch to ranges?

Contributor
Posts: 63

Re: Specifying character ranges

Excellent point! Yes,  luckily my values are clean, so I won't have to worry about garbage in the ranges.

 

Thanks!

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 420 views
  • 2 likes
  • 3 in conversation