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

I have two dates:

Entry Date

Given Date

I want to create a new variable, date_match = 1 when entry_date = given_date

I have been using the following code;

if entry_date = given_date then date_match = 1;

but the date_match variable comes up as missing.

I am doing something incorrectly, but am not sure what.

Thank you for any assistance you can provide.

********************

I have attached some of the date data. There are records where the dates match and other where they do not. Thank you.

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

I just noticed the spreadsheet now attached to your original post. Entry_Date is a datetime variable, while Given_Date is a date variable.

Change your test to

date_match=datepart(entry_date) eq given_date;

View solution in original post

19 REPLIES 19
art297
Opal | Level 21

There are a number of reasons why the value would come up missing.  See if any of the following might apply:

You misspelled on or both of the variable names

You don't have any records where they match

One of the variables is a SAS date and the other is a datetime

One or both variables are something other than SAS date variables

Does your log provide any clues?

And, if you want to have zeros and ones as values, once you identify the source of your current problem, you can simplify the statement with:

date_match=entry_date eq given_date;

HyunJee
Fluorite | Level 6

The log does not show any errors, which causes me to be puzzled as to why the code is not working.

Both dates are in the "mmddyy10." format according to the proc contents.

I used datdif to calculate the number of days difference between the two dates and was able to get the results I needed, though I still would like to be to create a variable that simply identifies if they are different.

Would it be helpful to change the formatting of the dates?

Linlin
Lapis Lazuli | Level 10

please make sure the two date  variables are not defined as character variables.

when entry_date not equal given_date , date_match will come up as missing;

HyunJee
Fluorite | Level 6

They are both number variables in the mmddyy10. format

Linlin
Lapis Lazuli | Level 10

Can you provide us a sample of the data you work with?

HyunJee
Fluorite | Level 6

I have attached some of the data. Thanks.

art297
Opal | Level 21

I don't see any attached data.

art297
Opal | Level 21

I just noticed the spreadsheet now attached to your original post. Entry_Date is a datetime variable, while Given_Date is a date variable.

Change your test to

date_match=datepart(entry_date) eq given_date;

Linlin
Lapis Lazuli | Level 10

data have;

informat  entry_date given_date mmddyy10.;

format entry_date given_date mmddyy10.;

input entry_date given_date;

date_match=entry_date eq given_date;

cards;

12/1/2011 12/20/2011

12/15/2011 12/15/2011

;

proc print ;run;

ballardw
Super User

Try

date_match= (entry_date eq given_date);

I use this often. You will get a 0 for date_match when not equal.

UrvishShah
Fluorite | Level 6

Hi

May be your data contains white space between two dates values

Try following

data test;

input entry_date ddmmyy10. +1 given_date ddmmyy10.;

cards;

01/10/2000 01/10/2000

01/11/2001 01/12/2003

;

Make sure that your data not contains white space and if it contains space then make sure that you include space while reading data

data test;

set test;

if entry_date = given_date then date_match = 1;

run;

If you try the above code , you'll get your output

Regards

Tom
Super User Tom
Super User

If your dataset is small (less than a couple of hundred obs) then print it out and check if any dates actually match.

If it is large here is a trick to see if you can find examples of matches or non matches.

title1 'Matches';

proc print data=have (obs=10) ;

   where entry_date = given_date ;

run;

title1 'Non-Matches';

proc print data=have (obs=10) ;

  where entry_date ne given_date ;

run;

title1 ;

VX_Xc
Calcite | Level 5

First save your excel file type as CSV (comma delimited) file type to you desktop(or other folder).

Second Run below;

Data datedata;

      Infile 'C:\Desktop\Date data.csv' DLM = ',' FIRSTOBS = 2 ;

      Input Given_Date MMDDYY10. Sex $ Vaccine :$20. Entry_Date MMDDYY10.;

      Match_Date = Given_Date - Entry_Date + 1 ;

      FORMAT Given_Date Entry_Date MMDDYY10.;

run;

Check;

TITLE;

proc print data = datedata NOOBS;

run;

Given_Date

Sex

Vaccine

Entry_Date

Match_Date

12/01/2009

M

DTaP

12/01/2009

1

12/01/2009

M

Hiberix

12/01/2009

1

12/01/2009

F

H1N1 Sanofi(6-35m)

12/02/2009

0

12/01/2009

M

H1N1 Nov 4y and up

12/01/2009

1

12/01/2009

M

DTaP-HepB-IPV

12/01/2009

1

art297
Opal | Level 21

You may have missed my last post:

I just noticed the spreadsheet now attached to your original post. Entry_Date is a datetime variable, while Given_Date is a date variable.

Change your test to

date_match=datepart(entry_date) eq given_date;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 19 replies
  • 3188 views
  • 4 likes
  • 7 in conversation