BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
InspectahDex
Obsidian | Level 7

How do I write not equal to a negative number? I want to include zeros, positive numbers, and null values. Basically I want to only exclude anything that has "-" in front of a number.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Since you want to include the missing values you will need to test for missing values separately.

where x>=0 or missing(x) ;

If you wanted to exclude positive values (keeping missing and negative values) it would be easier because missing values are smaller than all actual numbers so you could just use:

where x <=0 ;

So you could use negate the value and use that test:

2125  data _null_;
2126    do x=.,.z,-1,0,1 ;
2127      test1 = (x >= 0) or missing(x);
2128      test2 = -x <= 0 ;
2129      put x= test1= test2= ;
2130    end;
2131  run;

x=. test1=1 test2=1
x=Z test1=1 test2=1
x=-1 test1=0 test2=0
x=0 test1=1 test2=1
x=1 test1=1 test2=1
NOTE: Missing values were generated as a result of performing an operation on missing values.
      Each place is given by: (Number of times) at (Line):(Column).
      2 at 2128:14
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds

But notice that it causes notes to be generated when there are missing values since it is performing the negation operation on the value.

View solution in original post

4 REPLIES 4
SASKiwi
PROC Star

There is no such thing as null values in SAS. I assume you mean missing values. This should do it:

where MyNumber = . or MyNumber >= 0;
Tom
Super User Tom
Super User

Since you want to include the missing values you will need to test for missing values separately.

where x>=0 or missing(x) ;

If you wanted to exclude positive values (keeping missing and negative values) it would be easier because missing values are smaller than all actual numbers so you could just use:

where x <=0 ;

So you could use negate the value and use that test:

2125  data _null_;
2126    do x=.,.z,-1,0,1 ;
2127      test1 = (x >= 0) or missing(x);
2128      test2 = -x <= 0 ;
2129      put x= test1= test2= ;
2130    end;
2131  run;

x=. test1=1 test2=1
x=Z test1=1 test2=1
x=-1 test1=0 test2=0
x=0 test1=1 test2=1
x=1 test1=1 test2=1
NOTE: Missing values were generated as a result of performing an operation on missing values.
      Each place is given by: (Number of times) at (Line):(Column).
      2 at 2128:14
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds

But notice that it causes notes to be generated when there are missing values since it is performing the negation operation on the value.

ballardw
Super User

Provide some examples of values an results of how you are using this.

There are often several ways to accomplish things in SAS and know what you want and how that result is used might point us to a more flexible method than one that "works" for a very small use case.

Ksharp
Super User
/*Or try SIGN() function*/
data have;
input x;
if sign(x)=-1 then delete;
cards;
98
0
.
-98
;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 4 replies
  • 504 views
  • 2 likes
  • 5 in conversation