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.
I used this code as the solution:
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.
There is no $ before DATE.
Show us your code. Error messages by themselves without seeing the code are not particularly useful.
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.
@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));
I used this code as the solution:
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.);
Thanks everyone for all of the assistance. I will try all of the suggested changes tomorrow and revert.
Thank you, everyone.😊
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;
Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.
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.