compare two variables in same dataset

Solved
Occasional Contributor
Posts: 19

compare two variables in same dataset

Have a dataset with three columns

have dataset

dob1                dob2            rank

10/18/2017  10/27/1998      1

10/09/2017  11/03/1982      2

09/10/1991   09/10/1991     2

needed dataset

dob1                dob2            rank

10/18/2017  10/27/1998      1

10/09/2017  11/03/1982      2

09/10/1991   09/10/1991     null

if dob1=dob2 then ran=null ; else rank=rank;

both dob1 and dbo2 are date formats (type=date and length 8)

Accepted Solutions
Solution
‎11-13-2017 01:17 PM
SAS Super FREQ
Posts: 37

Re: compare two variables in same dataset

Hi @RAVI2!

If your "rank" variable is categorical, you can do what your describing with the code below:

``````data have;
input @1 dob1 mmddyy10. @12 dob2 mmddyy10. @23 rank \$;
datalines;
10/18/2017 10/27/1998 1
10/09/2017 11/03/1982 2
09/10/1991 09/10/1991 2
;
run;

data need;
set have;
if dob1=dob2 then rank ="null";
run;``````

If your "rank" variable is numeric, and by "null" you mean you want the entry to be missing, you can use the following code:

``````data have;
input @1 dob1 mmddyy10. @12 dob2 mmddyy10. @23 rank;
datalines;
10/18/2017 10/27/1998 1
10/09/2017 11/03/1982 2
09/10/1991 09/10/1991 2
;
run;

data need;
set have;
if dob1=dob2 then rank =.;
run;``````

Hope that helps!

All Replies
Super User
Posts: 22,041

Re: compare two variables in same dataset

Did you try the code you've shown? If it didn't work, please explain how.

You've misspelled rank and you specify null in SAS with the period ( . ) or use CALL MISSING instead.

Occasional Contributor
Posts: 19

Re: compare two variables in same dataset

Hi Reeza,

Getting the below error.

Statement is not valid or it is used out of proper order.

Haven't misspelled rank, while I'm coding. Sorry it's a typo mistake in the post.

But my doubt is can we use dob1=dbo2?

As dob1 doesn't have a value of dob2, I don't think it is proper syntax.

So, definitely it's a syntax error. And I need to know what would be the right one.

Thanks again.

Super User
Posts: 22,041

Re: compare two variables in same dataset

You're stating there's errors but we can't see them or your data or your code so it's a guessing game, which I don't really want to play.

Good Luck.

Occasional Contributor
Posts: 19

Re: compare two variables in same dataset

dob1            dob2     rank
10/18/2017 01/27/1998  1
10/09/2017 11/03/1982  3
08/23/2017 04/09/1999  1
10/23/2017 05/20/1988  2
10/18/2017 07/12/1999  1
09/25/2017 09/25/2017  1
09/25/2017 09/25/2017  1
09/26/2017 09/26/2017  3

Log

data get;
set have;
if dofb eq dob1 then score is null;
else rank=rank;
run;

Log

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: The SAS System stopped processing this step because of errors.

SAS Super FREQ
Posts: 37

Re: compare two variables in same dataset

Hi @RAVI2, where you have "score is null" you should have either "rank="null"" or "rank=." Check out my solution below.

Solution
‎11-13-2017 01:17 PM
SAS Super FREQ
Posts: 37

Re: compare two variables in same dataset

Hi @RAVI2!

If your "rank" variable is categorical, you can do what your describing with the code below:

``````data have;
input @1 dob1 mmddyy10. @12 dob2 mmddyy10. @23 rank \$;
datalines;
10/18/2017 10/27/1998 1
10/09/2017 11/03/1982 2
09/10/1991 09/10/1991 2
;
run;

data need;
set have;
if dob1=dob2 then rank ="null";
run;``````

If your "rank" variable is numeric, and by "null" you mean you want the entry to be missing, you can use the following code:

``````data have;
input @1 dob1 mmddyy10. @12 dob2 mmddyy10. @23 rank;
datalines;
10/18/2017 10/27/1998 1
10/09/2017 11/03/1982 2
09/10/1991 09/10/1991 2
;
run;

data need;
set have;
if dob1=dob2 then rank =.;
run;``````

Hope that helps!

Occasional Contributor
Posts: 19