DATA Step, Macro, Functions and more

Comparing dates with if statment

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 8
Accepted Solution

Comparing dates with if statment

Hi everyone,

 

I'm still pretty new to sas.

Here is the situation.

 

I have a table (Merged1) that has IDnumbers some descriptions and 2 dates, an event date and a consent date. both of the dates are in the format MMDDYY10 and each row has all these variables.

I want to get a data set where the consent date is before the event date. So if an even happened before the consent it will not be in my new data set. I have something like this...

 

data Sample1;

    set merged_data;

    where event_date > consent_date;

run;

 

I know that dates are just numbers counting from a certain date, so I would imagine this would work, but that is not the case.

 

please and thank you

 

 


Accepted Solutions
Solution
‎03-14-2017 05:12 PM
PROC Star
Posts: 7,360

Re: Comparing dates with if statment

If your two dates are just character strings that look like dates, just change your code to:

 

data Sample1;
    set merged_data;
    if input(event_date,mmddyy10.) > input(consent_date,mmddyy10.);
run;

HTH,

Art, CEO, AnalystFinder.com

 

 

 

View solution in original post


All Replies
Super User
Posts: 6,932

Re: Comparing dates with if statment

Make sure (proc contents) that the date variables are numeric with a MMDDYY10. assigned.

If that is the case, post some examples that illustrate your issues in a data step with datalines.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 17,784

Re: Comparing dates with if statment


Curly wrote:

Hi everyone,

 

I would imagine this would work, but that is not the case.

 

please and thank you

 

 


What does that mean? How did it not work?

Solution
‎03-14-2017 05:12 PM
PROC Star
Posts: 7,360

Re: Comparing dates with if statment

If your two dates are just character strings that look like dates, just change your code to:

 

data Sample1;
    set merged_data;
    if input(event_date,mmddyy10.) > input(consent_date,mmddyy10.);
run;

HTH,

Art, CEO, AnalystFinder.com

 

 

 

Super User
Posts: 10,483

Re: Comparing dates with if statment

Example data.

Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.

Frequent Contributor
Posts: 117

Re: Comparing dates with if statment

You need to look into the descriptor portion about how the dates are formated. Use PROC CONTENTS and see weather the dates are char or num and use appropriate method as mentioned by others.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 179 views
  • 2 likes
  • 6 in conversation