Help using Base SAS procedures

How do I compare two fields one with leading zeros one without, however some will also have alpha ..

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 90
Accepted Solution

How do I compare two fields one with leading zeros one without, however some will also have alpha ..

Hello all,

I have two Data Bases that store a field room, one has it with leading zeros and one without, the hitch is these rooms can have a alpha charater?   any ideas?

I had some 1100 false positives now with this just a 100 or so

 

I came up with this:

put(input(a.room,best4.),z4.) <> put(input(b.room,best4.),z4.)

 

I started with:

a.room <> b.room

but with data like:

 

a.room     b.room

451a        0451a

451b        0451b

1              00001

21            00021

1-1           001-1

 

 

 

I am not sure what to try to get them down to the true diffences that I want.  TIA   -KJ


Accepted Solutions
Solution
‎08-12-2016 10:49 AM
Respected Advisor
Posts: 3,124

Re: How do I compare two fields one with leading zeros one without, however some will also have alph

PRX functions can be handy,

data have;
input aroom$     broom$;
_b=prxchange('s/(^0*)([^0]+.+)/$2/o',-1,broom);
equal_or_not=ifc(aroom=_b,'Y','N');
cards;
451a        0451a
451b        0451b
1              00001
21            00021
1-1           001-1
451a        0451c
;


View solution in original post


All Replies
Solution
‎08-12-2016 10:49 AM
Respected Advisor
Posts: 3,124

Re: How do I compare two fields one with leading zeros one without, however some will also have alph

PRX functions can be handy,

data have;
input aroom$     broom$;
_b=prxchange('s/(^0*)([^0]+.+)/$2/o',-1,broom);
equal_or_not=ifc(aroom=_b,'Y','N');
cards;
451a        0451a
451b        0451b
1              00001
21            00021
1-1           001-1
451a        0451c
;


Frequent Contributor
Posts: 90

Re: How do I compare two fields one with leading zeros one without, however some will also have alph

Sorry I did not say it but my comparison I was using is in a proc sql where, I will check out your function. I hope it works in a proc sql.
Super User
Posts: 9,681

Re: How do I compare two fields one with leading zeros one without, however some will also have alph

data have;
input aroom$     broom$;
equal_or_not=(aroom=prxchange('s/^0+//',-1,broom));
cards;
451a        0451a
451b        0451b
1              00001
21            00021
1-1           001-1
451a        0451c
;
run;

Frequent Contributor
Posts: 90

Re: How do I compare two fields one with leading zeros one without, however some will also have alph

Both answers work equally well for my needs tie goes to the first response. thanks for your pointer on "prxchange"
Super User
Posts: 17,829

Re: How do I compare two fields one with leading zeros one without, however some will also have alph

Just a check, but You know that <> is max in SAS?

In most languages it means Not Equals and from your usage it looks like that's what your using it for?  

 

The MIN and MAX operators are used to find the minimum or maximum value of two quantities. Surround the operators with the two quantities whose minimum or maximum value you want to know. The MIN (><) operator returns the lower of the two values. The MAX (<>) operator returns the higher of the two values.

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

Frequent Contributor
Posts: 90

Re: How do I compare two fields one with leading zeros one without, however some will also have alph

[ Edited ]

Yes I am aware that <> is max, but in "proc sql" it works and works with SQL queries diectly thus we use it...   I have been burned by it in data steps so it is playing a bit with fire. 

☑ This topic is SOLVED.

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

Discussion stats
  • 6 replies
  • 1702 views
  • 7 likes
  • 4 in conversation