BookmarkSubscribeRSS Feed
eroolpal
Fluorite | Level 6

I have extracted the date from a file and it is in the format of 09-30-21 and I have to convert this to the format 30Sep2021. Is there a way I could do this?

9 REPLIES 9
MarkusWeick
Barite | Level 11

Hi @eroolpal,

you could try the format statement (SAS Help Center: FORMAT Statement) in a data step with the format "date9.".

Best

Markus

 

Please keep the community friendly.
Like posts you agree with or like. Mark helpful answers as “accepted solutions”. Generally have a look at https://communities.sas.com/t5/Getting-Started/tkb-p/community_articles
pink_poodle
Barite | Level 11
If 09-30-21 is not a sting but a SAS date in DDMMYYD. format,
you can just apply date9. format to get 30Sep2021:
https://support.sas.com/documentation/cdl/en/lrcon/65287/HTML/default/viewer.htm#p1wj0wt2ebe2a0n1lv4...
eroolpal
Fluorite | Level 6
Thanks. Its in the string format. how do I convert that to a date format first.
Tom
Super User Tom
Super User

@eroolpal wrote:

I have extracted the date from a file and it is in the format of 09-30-21 and I have to convert this to the format 30Sep2021. Is there a way I could do this?


What do you mean by "extracted"?  What do you mean by "file"?

If you using a data step to read a text file then using the MMDDYY informat to read text like 09-30-21 into dates.  If your date strings really only have two digits for the year you could have trouble. https://en.wikipedia.org/wiki/Year_2000_problem 

 

Once you have a variable with date values (number of days since 1960) then you can use any of the various SAS formats that display date values. Looks like you want the DATE9 format which would display that date as 30SEP2021.

eroolpal
Fluorite | Level 6
yes I do have a file. I am extracting it as a string and then will want to change this to a date format and then need to change it to 30Sep2021
pink_poodle
Barite | Level 11
data want;
informat A DDMMYYD. ;
input A;
format A date9.;
cards;
09-30-21
run;
https://support.sas.com/resources/papers/proceedings/proceedings/sugi24/Coders/p073-24.pdf
eroolpal
Fluorite | Level 6

Thanks. this code worked for me

 

data work.ds;
date_string = "09-30-21";
date_number = input(date_string, MMDDYY8.);
format date_number date9.;
run;

Tom
Super User Tom
Super User

@eroolpal wrote:
yes I do have a file. I am extracting it as a string and then will want to change this to a date format and then need to change it to 30Sep2021

If you already have a character variable, let's call it STRING, then use the INPUT() function with an appropriate informat specification to convert that string into a number that SAS can treat as a date.  Then attach a date type format to it so it prints in a style that humans will recognize as a date.

 

So if your existing dataset is named HAVE here is how you could make a new dataset and a new variable.

data want;
  set have;
  date = input(string,mmddyy11.);
  format date date9.;
run;

Note that you cannot change the type of an existing variable.  So it you want the new variable to use the name that is already being used by the character variable then add a RENAME (and possibly and DROP) statement.  So if the existing variable named DATE and you want to use DATE as the name of the new numeric variable you might use this data step.

data want;
   set have;
   date_num = input(date,mmddyy11.);
   format date_num date9.;
   rename date_num=date date=date_string ;
run;

To fix the way you are creating the original dataset so that the variable is defined as a date to begin with you need to explain what you mean by the words "extract" and "file".  If you have a text file and you want to create a SAS dataset from it then use a data step with an INFILE statement and appropriate INPUT statements.  If you read the string from the text file use the MMDDYY informat to start with then the variable you create will have date values already.

 

Note that in addition to being precise about what you mean by "extract" and "file" you should be precise about what you mean by "format".  The word FORMAT in SAS has a specific meaning that is different than they way you are using it.  SAS has only two types of variables, floating point numbers and fixed length character strings.  SAS stores DATE, TIME and DATETIME values as numbers. So a variable does not have a "date format".  Instead it contains date values.  You could attach a date type format to the variable so that the values display in a way that humans will recognize as a date.

 

In SAS a FORMAT is instructions for how to display values as text.  A numeric format (like the DATE format) displays numbers as text.  A character format displays character values as text.  SAS also has something called in an INFORMAT.  An informat converts text into values.  A numeric informat converts the text into a number (like the MMDDYY informat). A character informat converts text into character values. 

 

 

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!
How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 9 replies
  • 1871 views
  • 8 likes
  • 5 in conversation