BookmarkSubscribeRSS Feed
marleeakerson
Calcite | Level 5

Hello, 

 

I think I have a very simple question using date variables but I can't seem to figure it out. I am working with a date variable in this format 

 

Date

02/09/1996
02/09/1996
02/09/1996
06/19/1981
10/31/1954
10/09/1958
06/17/1960

 

and I am trying to find all the variables where the Date is after October 1, 2019. So I am using this piece of code: 

 

if Date >= 10/01/2019
then Variable= "X";

 

But I keep getting observations where the Date is before 10/01/2019 marked as "X". 

 

Any suggestions? 

2 REPLIES 2
mklangley
Lapis Lazuli | Level 10

If Date is a SAS date variable, use this:

if Date ge '01Oct2019'd
    then variable = 'X';

This will compare Date to a SAS date variable. (That is how you write a date as a SAS date variable: written as DDMonYYYY, with ' before and 'd after. Such as '17Feb1982'd)

 

If Date is a character string, use this:

if input(Date,mmddyy10.) ge '01Oct2019'd
    then variable = 'X';

That will convert the Date string to a SAS date value to compare with '01Oct2019'd.

ballardw
Super User

SAS date valued variables are numeric and should have a format such as MMDDYY10. assigned.

If your date variable is an actual SAS date value the code should be

 

if Date >= '01OCT2019'd
then Variable= "X";

 

Literal dates must be in the date9 or date7(not recommended) 01OCT2019 format, in quotes and with the D to tell SAS that the value is intended to be a date.

In some locals 10/01/2019 would be 10 January 2019 and if you do something like 01/02/03 it is impossible to tell which might actually be the year. So SAS uses the 'ddmmmyyyy'd as a way to know which specific date is intended.

 

What your code is actually doing is telling SAS to divide 10 by 1, then divide that result by 2019.

You could test that with code like:

data junk;
  date='01JUN2018'd;
  z = 10/01/2019;
  if Date >= z then Variable= "X";
  if date >='01OCT2019'd then var2 = 'X';
run;

A value of Z in that range will be considered to be 01Jan1960.

 

https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/... has a PDF with much information about dates.

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 340 views
  • 0 likes
  • 3 in conversation