DATA Step, Macro, Functions and more

Inequality signs issue

Accepted Solution Solved
Reply
Contributor
Posts: 34
Accepted Solution

Inequality signs issue

My data is as follows:

ID [num var]           Percent [Character var]
1452                      10
1254                      20.00
4852                      30
7526                      40
7542                      0
7530                      50.00
9856                      50
3256                      51
7854                     100
7128                      500
1287                      80
4568                      5.00
7540                      8.00
9530                      9
9920                      25

 

I'm trying to create another dataset from the above data where the percent is larger than 0 and less than or equal to 50.

 

I used 

proc sql;
create table x as
select id, percent from test where "0"<percent<="50";
quit;

 

but it gives me the observation that has 100 percent.

 

Please help!

 

 


Accepted Solutions
Solution
‎05-20-2018 05:40 PM
Super User
Posts: 6,634

Re: Inequality signs issue

Posted in reply to pmpradhan

As you have seen, SAS compares character strings differently.  Try it this way:

 

where 0 < input(percent, 8.) <= 50

View solution in original post


All Replies
Solution
‎05-20-2018 05:40 PM
Super User
Posts: 6,634

Re: Inequality signs issue

Posted in reply to pmpradhan

As you have seen, SAS compares character strings differently.  Try it this way:

 

where 0 < input(percent, 8.) <= 50

Contributor
Posts: 34

Re: Inequality signs issue

Posted in reply to Astounding
Thank you but I got this error when implementing "where 0 < input(percent, 8.) <= 50 " ERROR: Syntax error while parsing WHERE clause.
Super User
Posts: 6,634

Re: Inequality signs issue

Posted in reply to pmpradhan

If you really want help with this, post the log.  It should be relatively tiny, and the solution should be relatively simple.  "Syntax error" doesn't say enough.

Contributor
Posts: 34

Re: Inequality signs issue

Posted in reply to Astounding
Thank you so much! this worked!
PROC Star
Posts: 1,584

Re: Inequality signs issue

Posted in reply to pmpradhan

remove the quotes in  "0"<percent<="50";  and try again. I believe the auto conversion should take effect 

Contributor
Posts: 34

Re: Inequality signs issue

Posted in reply to novinosrin
Tried it didn't work. Smiley Sad
PROC Star
Posts: 1,584

Re: Inequality signs issue

Posted in reply to pmpradhan

ok sorry,no worries as you have @Astounding converted solution. 

Contributor
Posts: 34

Re: Inequality signs issue

Posted in reply to novinosrin
Well that didn't work as well.
PROC Star
Posts: 1,584

Re: Inequality signs issue

Posted in reply to pmpradhan

 

please post your log 

 

 

data have;
do var='1','2','3','4','5';
output;
end;
run;
proc sql;
create table want as
select *
from have
where 2<input(var,8.)<=4;
quit;

Contributor
Posts: 34

Re: Inequality signs issue

Posted in reply to novinosrin
Yes, this worked! Thank you! Smiley Happy
PROC Star
Posts: 1,288

Re: Inequality signs issue

Posted in reply to pmpradhan

Not tested, but I don't believe that comparison format works in a SQL query. Try

 

"0" < percent and percent <="50"

 

Also, if your "percent" variable is character, it is VERY likely you'll get unexpected results from a character comparison. Test carefully! If "percent" is numeric, remove the double quotes from 0 and 50.

 

Tom

Contributor
Posts: 34

Re: Inequality signs issue

Tried but this also gives me the observation that has percent =100.
☑ This topic is solved.

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

Discussion stats
  • 12 replies
  • 190 views
  • 2 likes
  • 4 in conversation