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

Hi I want to convert multiple variables in iso to DD-MMM-YYYY (18JUNE2018).

it also have partial dates for some date values 

is there any method to do that?

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@ambadi007 wrote:
the dates will be
2018-08-26
2018-07 (partial date day missing)- so this should be 2018-Jul
2019 (month and day missing) etc... this should be 2019

Since the value SAS will create is a complete date what day of the month should be imputed for the Year month? And what month and day should be imputed when only the year is present?

 

You are very likely much better off creating a SAS date value instead of a character variable because 1) SAS dates sort properly, 2) you can change appearance with format changes without having to recreate a bunch of new variables and 3) the functions that SAS supplies to manipulate dates, or that use dates in time-series analyis, expect the numeric SAS valued date.

 

The Anydtdte. informat will assume that 4 digits delimited is a year and guesses that the second is the month when no day is provided. The resulting date uses the first of the month.

Since there is no way for anyone to reliably know that a date of 4 digits is only a year you need to provide code to account for that case. The MDY (month day year) function creates a date value given the number of month, day and year. So I provide an example that will assume month and day are 1. If you want a different assumed day and and month  for only year values you can set it there.

 

data example;
   input datetext :$10.;
   date=input(datetext,anydtdte.);
   /* the following assumes the month,first parameter is 1 and day is 1*/
   if length( datetext)=4 then date=mdy(1,1,input(datetext,f4.));
   format date date11.;
datalines;
2018-08-26
2018-07 
2019
;

 

 

View solution in original post

11 REPLIES 11
ChrisNZ
Tourmaline | Level 20

> I want to convert multiple variables in iso

There are several ISO variations. Show what you have.

 

>it also have partial dates for some date values 

Even more reason to show your data

ambadi007
Quartz | Level 8
the dates will be
2018-08-26
2018-07 (partial date day missing)- so this should be 2018-Jul
2019 (month and day missing) etc... this should be 2019
ChrisNZ
Tourmaline | Level 20

What's the expected output? A SAS date? A string? What date do you want when the month or day are missing?

ambadi007
Quartz | Level 8

The dates should be in text.

if the date are full the output date should be looks like

 

29Jun2020

 

if month missing

 

29---2020

 

if day missing

 

Jun2020

 

if day and month missing 

2020 

ChrisNZ
Tourmaline | Level 20

29Jun2020

29---2020

Jun2020

2020 


Can you tell how are you going to use these values? I am curious now.

 

For more typical outcome, here is another method:

 

data HAVE;
  A='2018-08-26'; output;
  A='2018-07   '; output;
  A='2019      '; output;
run;

proc format;
  invalue anydt (default=24)
    's/\A(\d{4}) *\Z/\1-01-01/  ' (regexpe)= [yymmdd10.] 
    's/(\A\d{4}-\d\d) *\Z/\1-01/' (regexpe)= [yymmdd10.] 
    'm/\A\d{4}-\d\d-\d\d *\Z/   ' (regexp) = [yymmdd10.] 
  other                                   =  .;
run;     

data WANT;
  set HAVE;
  B = input(A,anydt.); putlog B=;
  format B date9. ;
run;

B=26AUG2018
B=01JUL2018
B=01JAN2019

 

 

 

 

ballardw
Super User

@ambadi007 wrote:
the dates will be
2018-08-26
2018-07 (partial date day missing)- so this should be 2018-Jul
2019 (month and day missing) etc... this should be 2019

Since the value SAS will create is a complete date what day of the month should be imputed for the Year month? And what month and day should be imputed when only the year is present?

 

You are very likely much better off creating a SAS date value instead of a character variable because 1) SAS dates sort properly, 2) you can change appearance with format changes without having to recreate a bunch of new variables and 3) the functions that SAS supplies to manipulate dates, or that use dates in time-series analyis, expect the numeric SAS valued date.

 

The Anydtdte. informat will assume that 4 digits delimited is a year and guesses that the second is the month when no day is provided. The resulting date uses the first of the month.

Since there is no way for anyone to reliably know that a date of 4 digits is only a year you need to provide code to account for that case. The MDY (month day year) function creates a date value given the number of month, day and year. So I provide an example that will assume month and day are 1. If you want a different assumed day and and month  for only year values you can set it there.

 

data example;
   input datetext :$10.;
   date=input(datetext,anydtdte.);
   /* the following assumes the month,first parameter is 1 and day is 1*/
   if length( datetext)=4 then date=mdy(1,1,input(datetext,f4.));
   format date date11.;
datalines;
2018-08-26
2018-07 
2019
;

 

 

ambadi007
Quartz | Level 8

As of now I dont want to impute anything for partial dates.

 

full date- 29Jun2020

if day missing it can be like --Jun2020

if month is missing 29---2020

if day and month is missing then only year 2020 needed.

 

this is the requirement needed 

 

andreas_lds
Jade | Level 19

@ambadi007 wrote:

As of now I dont want to impute anything for partial dates.

 

full date- 29Jun2020

if day missing it can be like --Jun2020

if month is missing 29---2020

if day and month is missing then only year 2020 needed.

 

this is the requirement needed 

 


Then you don't have a date, but a hardly usable text-variable.

ballardw
Super User

@ambadi007 wrote:

As of now I dont want to impute anything for partial dates.

 

full date- 29Jun2020

if day missing it can be like --Jun2020

if month is missing 29---2020

if day and month is missing then only year 2020 needed.

 

this is the requirement needed 

 


And you have now introduced yet another type of data error -the missing month with day.

 

PLEASE explain how 29---2019 is more usable than 2019-  -29, or however that would have originated. At least the problematic value you have now will sort somewhat, your proposed result won't do that.

 

ballardw
Super User

SAS has about 20 different ISO related informats and formats because there are that many "standards". You need to show us some actual examples of the values you have for best advice.

 

And specifically show what the "partial dates" as well look like. As I understand ISO such a thing can't actually occur if the value is actually ISO.

Copy the values from a TEXT document as and paste the results into a code box opened with the </> icon on the forum. That will preserve formatting. Anything pasted into the main message windows will get reformatted, usually loosing white space characters and such which could invalidate any code suggestions for your actual data.

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 11 replies
  • 3974 views
  • 3 likes
  • 5 in conversation