BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
zimu94681
Fluorite | Level 6

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!!!

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

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;

View solution in original post

6 REPLIES 6
novinosrin
Tourmaline | Level 20

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;
Tom
Super User Tom
Super User

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?

PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
zimu94681
Fluorite | Level 6

Does it mean that it's not a solvable situation? SAS can't distinguish negative values from missing value?

Tom
Super User Tom
Super User

@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

 

PaigeMiller
Diamond | Level 26

@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.

--
Paige Miller

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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