BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
einstein
Quartz | Level 8

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

8 REPLIES 8
Reeza
Super User

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?

einstein
Quartz | Level 8

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.

Reeza
Super User

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?

einstein
Quartz | Level 8

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.

Reeza
Super User

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;
einstein
Quartz | Level 8

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

Astounding
PROC Star

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?

einstein
Quartz | Level 8

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

 

Thanks!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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