Delete a date

Accepted Solution Solved
Reply
Contributor
Posts: 43
Accepted Solution

Delete a date

Hi,

I'm trying to delete the date from my dataset and I used this statement to delete but it is not working.

 

data new;

set have;

if date eq 2/11/17 then delete;

run;

 

It is not working, I have the same format for date in my dataset and it is a numeric var.

I need to delete 2/11/17 and 2/12/17, I tried with 2/11/17 first and is nto working.

 

Any help?

 

Thank you

M

 


Accepted Solutions
Solution
‎03-02-2017 09:26 PM
SAS Super FREQ
Posts: 8,720

Re: Delete a date

Hi:

  If your date is internally stored as a numeric variable which represents the number of days since January 1, 1960 then the code shown in the screen shot should work. The DELETE statement, does work correctly, as illustrated by this sample program with 3 rows of "fake" data.

 

delete_works.png

 

  If your program does not work then there are some possibilities to investigate, as suggested:

1) if your variable is a numeric variable which represents the number of days since Jan 1, 1960, then you need to use a date constant in the IF statement;

2) If your variable is a character string that just looks like a date, but is not a number, then you need to either convert your character string to a numeric variable (using an INPUT function); use a character comparison;

3) if your variable is really a datetime value (like 11feb2017:01:15:30) then you need to use a different function to extract only the date portion from the datetime value;

4) if your variable is a number, like 20170211 or 02112017, then you will need to use different functions to turn the "plain" number into a SAS date value.

 

  Hope this illustrates that the DELETE statement does work, when your IF statement is valid.

 

cynthia

View solution in original post


All Replies
Super Contributor
Posts: 406

Re: Delete a date

[ Edited ]

This will probably work better:

 

data new;
   set have;
   if date eq "11FEB2017"d then delete;
run;

In SAS you can specify a date constant by a date9 formatted string between quotes and a 'd' behind it. Look up the docs for date, time and datetime literals.

 

Hope this helps,

- Jan.

 

Contributor
Posts: 43

Re: Delete a date

Hi Jan,

I tried that, I don't see any errors but I see the dates still exist in the dataset.

 

M

Super Contributor
Posts: 406

Re: Delete a date

Then it would help if you can show a sample of your data and the output of proc contents of the dataset you're working with. Maybe what you think to be a date is in fact not.

 

- Jan.

Solution
‎03-02-2017 09:26 PM
SAS Super FREQ
Posts: 8,720

Re: Delete a date

Hi:

  If your date is internally stored as a numeric variable which represents the number of days since January 1, 1960 then the code shown in the screen shot should work. The DELETE statement, does work correctly, as illustrated by this sample program with 3 rows of "fake" data.

 

delete_works.png

 

  If your program does not work then there are some possibilities to investigate, as suggested:

1) if your variable is a numeric variable which represents the number of days since Jan 1, 1960, then you need to use a date constant in the IF statement;

2) If your variable is a character string that just looks like a date, but is not a number, then you need to either convert your character string to a numeric variable (using an INPUT function); use a character comparison;

3) if your variable is really a datetime value (like 11feb2017:01:15:30) then you need to use a different function to extract only the date portion from the datetime value;

4) if your variable is a number, like 20170211 or 02112017, then you will need to use different functions to turn the "plain" number into a SAS date value.

 

  Hope this illustrates that the DELETE statement does work, when your IF statement is valid.

 

cynthia

☑ This topic is SOLVED.

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

Discussion stats
  • 4 replies
  • 212 views
  • 1 like
  • 3 in conversation