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

Hi All,

 

I hope everyone is keeping safe during these uncertain and difficult times of global pandemic.

 

Please assist me with some coding regarding dates. I am trying to change this date 2020-04-06 (character) to ''06Apr2020'' in sas. Please assist me.

1 ACCEPTED SOLUTION

Accepted Solutions
MagD
Quartz | Level 8

I used this code as the solution:

 

data fix_date;
set ECOM.Invoiced_Apps;
format Inv_Date date9.;
Inv_Date = mdy(substr(Invoice_Date,6,2),substr(Invoice_Date,9,2),substr(Invoice_Date,1,4));
run;
 
Thank you all for all of your assistance with this query.

View solution in original post

11 REPLIES 11
Reeza
Super User
sas_date = input(character_date, yymmdd10.);
format sas_date yymmdd10.;
*format sas_date date9.;

INPUT() converts the variable to a SAS date

FORMAT controls the appearance of the date, try un-commenting the third line to see the difference.


@MagD wrote:

Hi All,

 

I hope everyone is keeping safe during these uncertain and difficult times of global pandemic.

 

Please assist me with some coding regarding dates. I am trying to change this date 2020-04-06 (character) to ''06Apr2020'' in sas. Please assist me.


 

MagD
Quartz | Level 8
Hi Reeza,

I've used your recommended solution but I am getting the following error:
''Format $DATE was not found or could not be loaded''. This error is applicable to the date9.
PaigeMiller
Diamond | Level 26

There is no $ before DATE.

 

Show us your code. Error messages by themselves without seeing the code are not particularly useful.

--
Paige Miller
Tom
Super User Tom
Super User
You cannot attach a numeric format, DATE, to a character variable. When you do that SAS tries help fix your mistake by assuming you meant to use a character format. But it cannot find any format named $DATE. Make sure that the variable you are creating to store the date values is numeric.
Reeza
Super User
Did you change the variable name or try and use the same variable name? You cannot keep the same variable name or need to rename it.

Show your code.
MagD
Quartz | Level 8

Hi,

 

This is my code:

data want;
set invoices;
invoice_date = input(character_date, yymmdd10.);
format invoice_date date9.;
run;

 

The invoice_date variable is the variable that I am trying to change from 2020-03-03 to 03MAR2020 but I am getting the following error:

Format $date9. was not found or could not be loaded.

Tom
Super User Tom
Super User

@MagD wrote:

Hi,

 

This is my code:

data want;
set invoices;
invoice_date = input(character_date, yymmdd10.);
format invoice_date date9.;
run;

 

The invoice_date variable is the variable that I am trying to change from 2020-03-03 to 03MAR2020 but I am getting the following error:

Format $date9. was not found or could not be loaded.


Your comment makes it sound like INVOICE_DATE is the existing character variable. Your code is trying to create a NEW variable named INVOICE_DATE from an existing character variable named CHARACTER_DATE.  The error message is basically saying that INVOICE_DATE already existed and is a CHARACTER variable.  You also should have had notes about SAS converting the numeric values that the INPUT() function generate to text when it tried to store the results into the character variable.

 

Do you want to create a numeric date variable or not?  What name do you want this new variable to have?  Perhaps you just need to add a RENAME= dataset option on the reference to INVOICES in the SET statement.

set invoices(rename=(invoice_date=character_date));
MagD
Quartz | Level 8

I used this code as the solution:

 

data fix_date;
set ECOM.Invoiced_Apps;
format Inv_Date date9.;
Inv_Date = mdy(substr(Invoice_Date,6,2),substr(Invoice_Date,9,2),substr(Invoice_Date,1,4));
run;
 
Thank you all for all of your assistance with this query.
Tom
Super User Tom
Super User

If you just want leave it as a character variable but change how the dates are represented as character strings then add a PUT() function call to the assignment statement.

invoice_date = put(input(invoice_date , yymmdd10.),date9.);
MagD
Quartz | Level 8

Thanks everyone for all of the assistance. I will try all of the suggested changes tomorrow and revert.

 

Thank you, everyone.😊

Tom
Super User Tom
Super User

You can use INPUT() function (or INPUTN or INPUTC) to convert TEXT into VALUES.  The INFORMAT for converting strings like that is YYMMDD.  Once you have a date value you can attach the DATE format to have it displayed as text in the style you mentioned.

So if your source dataset is named HAVE and the strings are in the variable named CHAR then this data step will make a new dataset named WANT with a new numeric variable named NUM with the converted date values.

data want;
  set have;
  num = input(char,yymmdd10.);
  format num date9. ;
run;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 11 replies
  • 1796 views
  • 4 likes
  • 4 in conversation