BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
sanjaymane7
Obsidian | Level 7

Hi, I have a SAS dataset wherein date format is incorrect and the same is to be corrected. 

Please help in the matter.


data A;
input Employee_Id $ Date Date9. ;
cards;

70202028 - 04JAN2023;

70204018 - 04SEP2023;

70172038  - 27MAR2023;

run;

 

data want;

set a;

70202028 - 01APR2023; /*to be changed*/

70204018 - 09APR2023; /*to be changed*/

70172038  - 27MAR2023; /*this is correct and it should not be changed*/

run; 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Where did this data come from? How did you get it into SAS.

It looks like you mistakenly read in values in MDY order as if they were in DMY order (or the reverse).

 

You should probably fix the step that makes the data originally.

 

If you really do want to switch the month and day when the day is less than 13 then you could do something like:

data want ;
  set have;
  if day(date) in (1:12) then date=mdy(day(date),month(date),year(date));
run;

PS Your posted data step cannot work.  Do not add the hyphens and semicolons into the in-line data lines.  Do not use FORMATTED MODE to read the DATE value, use LIST MODE instead.

data have ;
  input Employee_Id $ Date :date. ;
  format date date9.;
cards;
70202028 04JAN2023
70204018 04SEP2023
70172038 27MAR2023
;

View solution in original post

4 REPLIES 4
yabwon
Onyx | Level 15

Base on what rules those dates should be changed ?

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Tom
Super User Tom
Super User

Where did this data come from? How did you get it into SAS.

It looks like you mistakenly read in values in MDY order as if they were in DMY order (or the reverse).

 

You should probably fix the step that makes the data originally.

 

If you really do want to switch the month and day when the day is less than 13 then you could do something like:

data want ;
  set have;
  if day(date) in (1:12) then date=mdy(day(date),month(date),year(date));
run;

PS Your posted data step cannot work.  Do not add the hyphens and semicolons into the in-line data lines.  Do not use FORMATTED MODE to read the DATE value, use LIST MODE instead.

data have ;
  input Employee_Id $ Date :date. ;
  format date date9.;
cards;
70202028 04JAN2023
70204018 04SEP2023
70172038 27MAR2023
;
sanjaymane7
Obsidian | Level 7

Hi, I got this readymade file from someone. I was trying to correct it. Thank you so much. Today is my review meeting and you saved my job. Thank you again

Tom
Super User Tom
Super User

@sanjaymane7 wrote:

Hi, I got this readymade file from someone. I was trying to correct it. Thank you so much. Today is my review meeting and you saved my job. Thank you again


So you have learned something.

1) Never use either MDY or DMY ordering when writing dates into text files (like CSV files).  Only use unambiguous styles like the DATE or YYMMDD formats produce.

2) Never use ANYDT... informats to read in date strings without first making sure that the DATESTYLE system option is set to the proper value for your data file.  Either MDY or DMY. 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 4 replies
  • 343 views
  • 3 likes
  • 3 in conversation