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

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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
  • 6 replies
  • 3134 views
  • 7 likes
  • 4 in conversation