BookmarkSubscribeRSS Feed
david27
Quartz | Level 8

Please run the below code and check for dataset have2

Why do I get value of flag as "2<1" when all values in nos2 is > nos1?

What am I missing?

 

Please advise.

Thanks

 

PROC FORMAT;
VALUE rvsd_format
	5000-10000="5000-10000"
	10000-25000="10000-25000"
;
run;

data have;
input nos1 nos2;
datalines ;	
9544.59 10605.10
8588.00 10781.20
9029.48 11061.31
9831.00 10158.70
9454.00 13380.18
9504.00 15888.00
8400.00 12728.80
6672.00 16516.00
7595.12 14062.24
9951.13 10780.39
;
run;
data have2;
set have;
fmt_nos1=put(nos1,rvsd_format.);
fmt_nos2=put(nos2,rvsd_format.);
if fmt_nos2=fmt_nos1 then flag="Equal";
else if fmt_nos2>fmt_nos1 then flag="2>1";
else flag="2<1";
run;
4 REPLIES 4
Jagadishkatam
Amethyst | Level 16

Here you are comparing the the character variables because of which it is taking the last else condition

flag="2<1"

 

 You need to compare the numeric variables

Thanks,
Jag
Jagadishkatam
Amethyst | Level 16

Please try the below code where I used the invalue for creating the informat as 1 and 2 and now you will get the expected result in the flag variable.

 

 

PROC FORMAT;
inVALUE rvsd_format
	5000-10000=1
	10000-25000=2
;
run;

data have;
input nos1 nos2;
datalines ;	
9544.59 10605.10
8588.00 10781.20
9029.48 11061.31
9831.00 10158.70
9454.00 13380.18
9504.00 15888.00
8400.00 12728.80
6672.00 16516.00
7595.12 14062.24
9951.13 10780.39
;
run;

data have2;
set have;
fmt_nos1=input(nos1,rvsd_format.);
fmt_nos2=input(nos2,rvsd_format.);
if fmt_nos2=fmt_nos1 then flag="Equal";
else if fmt_nos2>fmt_nos1 then flag="2>1";
else flag="2<1";
run;
Thanks,
Jag
Kurt_Bremser
Super User

The first character of "5000-10000" is "5", which comes later in the collating sequence than the first character of "10000-25000" ("1").

So "5000-10000" is "greater" than "10000-25000", when compared as strings.

Tom
Super User Tom
Super User

Not sure why are are comparing the strings generated by the format instead of the actual numbers. 

 

But why not just make sure the strings are formatted so they can be compared?

VALUE rvsd_format
   5000-10000="05000-10000"
  10000-25000="10000-25000"
;

sas-innovate-white.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.

 

Early bird rate extended! Save $200 when you sign up by March 31.

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 769 views
  • 2 likes
  • 4 in conversation