I have a dataset named "test" like this:
ID A B
1 1 1
2 1 1
3 1 0
4 0 1
5 -1 .
6 . -1
7 . .
8 . .
9 . .
10 . .
I wrote a program to define a new variable "c"
data test1;
set test;
if a>0 or b>0 then c=1;
else if a<0 and b<0 then c=99;
else c=.
run;
I wish to see observation 7-10, c=. (missing)
But using the codes I wrote, I got all observation from 5-10, c=99 (observation 5 and 6, I want their c to be 99, but not observations 7-10)
Can someone modify my codes and make observation 7-10 column c to missing?
Many Thanks!!!
HI @zimu94681
Corrected version
data test;
input ID A B;
cards;
1 1 1
2 1 1
3 1 0
4 0 1
5 -1 .
6 . -1
7 . .
8 . .
9 . .
10 . .
;
data test1;
set test;
if a>0 or b>0 then c=1;
else if .<a<0 or .<b<0 then c=99;
else c=.;
run;
HI @zimu94681
Corrected version
data test;
input ID A B;
cards;
1 1 1
2 1 1
3 1 0
4 0 1
5 -1 .
6 . -1
7 . .
8 . .
9 . .
10 . .
;
data test1;
set test;
if a>0 or b>0 then c=1;
else if .<a<0 or .<b<0 then c=99;
else c=.;
run;
Why not test for that first?
if missing(A) and missing(B) then c=.;
else if A>0 or B>0 then C=1;
else C=99;
Also what do you do when A or B is exactly zero?
Unstated in the above replies is that a missing numeric is considered to be less than zero. That's why your code produces a 99 for C. Either of the replies above have code to correct for this.
Does it mean that it's not a solvable situation? SAS can't distinguish negative values from missing value?
@zimu94681 wrote:
Does it mean that it's not a solvable situation? SAS can't distinguish negative values from missing value?
SAS has 28 missing values. All missing values are treated as smaller than any actual number and they actually are ordered. .Z is the largest missing value.
.Z < x < 0
@zimu94681 wrote:
Does it mean that it's not a solvable situation? SAS can't distinguish negative values from missing value?
That's not what it means. It means that missing is considered less than zero. Missing is also different than -1, and missing is different than -2, and so on.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.