DATA Step, Macro, Functions and more

How to display Dates in Character Format?

Accepted Solution Solved
Reply
PROC Star
Posts: 633
Accepted Solution

How to display Dates in Character Format?

Appreciate if someone of you help me understand to create the variable 'SDDTOP' and it should have the date value (last day of previous month)  with character format. 

 

e.g.  SDDTOP=2018-03-31


Accepted Solutions
Solution
‎04-20-2018 06:14 AM
Respected Advisor
Posts: 4,791

Re: How to display Dates in Character Format?

[ Edited ]

@Babloo wrote:
What if the value of 'have' is 2017-03-31 in numeric format and I just to
need to convert it to character? Can I use the put function with the
format $10.?

@Babloo

I would have thought you understand in-between how SAS Date and DateTime values work. If the value is numeric then it's either the count of days (date value) or seconds (datetime value) since 1/1/1960. You then apply a format to make this value human readable as a date/datetime value. This doesn't change the value itself which just remains a number, the format is just for printing.

If you really want to store such a numeric value representing a data/datetime in a character variable, then you have to use the put() function. This function does nothing else than applying the format you use to your numeric value and then write the result as a string - and if this is on the right side from the equal sign then you're assigning this string value to a character variable. In other languages the comparable function to a SAS put() is called write()  - and read() for input().

 

Can I use the put function with the format $10.?

I hope you understand now that the answer to this question is "no, of course not". You have to use the appropriate date/datetime format to write the number as a date/datetime string.

 

And last but not least: It's really MUCH better to keep dates as SAS date/datetime values (=numeric) with a format permanently assigned to the variable for printing/looking at it. Keeping the dates numeric allows you to actually use such data (i.e. if you want to calculated the number of days between two dates and the like; or even if you just want to sort by date). 

View solution in original post


All Replies
Super User
Posts: 10,569

Re: How to display Dates in Character Format?

Use the intnx function and a yymmddd10. format.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
PROC Star
Posts: 633

Re: How to display Dates in Character Format?

Posted in reply to KurtBremser

I already tried this, but it is in numeric format. I want it to be in character format.

Super User
Posts: 10,569

Re: How to display Dates in Character Format?

Post your code.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
PROC Star
Posts: 633

Re: How to display Dates in Character Format?

[ Edited ]
Posted in reply to KurtBremser

I tried this code and end up with the invalid values like ******** for the variable b and c.

 

 

data test;
a='31JAN2017:00:00:00'dt;
format a datetime23.;
b=PUT(a,date9.);
c=put(a,YYMMDD10.);
run;

My objective is to create two new variable based on the variable 'a'. One variable (e.g. 'b') should have value the 31JAN2017 in numeric format and another one (e.g. 'c') should have the value 2017-01-31 and it should be in character format.

 

Super User
Posts: 10,569

Re: How to display Dates in Character Format?

So you do not have a DATE value, as you told lyingly to us, but a DATETIME value. As you were told probably a hundred times now, a date value is a count of DAYS, while a datetime value is a count of SECONDS, so a datetime value is MUCH too big for a date format. There is a magic function called DATEPART that extracts the date out of a datetime. USE IT.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
PROC Star
Posts: 633

Re: How to display Dates in Character Format?

Posted in reply to KurtBremser

I managed to create one of the two variables using datepart function. However I'm seeking your help to create the variable and value as below (last day of previous month) and it should be in character format.  Challenge which i have here is to make this as a character variable.

 

SDDTOP = 2018-03-31

Super User
Posts: 10,569

Re: How to display Dates in Character Format?

put() function converts numeric to character.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Super User
Posts: 9,840

Re: How to display Dates in Character Format?

data test;
  a='31JAN2017:00:00:00'dt;
  format a datetime23.;
  b=put(datepart(a),date9.);
  c=put(datepart(a),yymmdd10.);
run;
Respected Advisor
Posts: 4,791

Re: How to display Dates in Character Format?

[ Edited ]

@Babloo wrote:

I managed to create one of the two variables using datepart function. However I'm seeking your help to create the variable and value as below (last day of previous month) and it should be in character format.  Challenge which i have here is to make this as a character variable.

 

SDDTOP = 2018-03-31


@Babloo

The way I went about this: 

1. Google search:   site:support.sas.com 9.4 formats and informats

That returns as the very first hit: https://support.sas.com/documentation/cdl/en/leforinforref/64790/HTML/default/viewer.htm 

2. Go to the documentation for Formats

3. Open up "Formats by category"

Capture.JPG

4. search for yyyy-   

....and this then led me to format e8601dn. which returns exactly what you're asking for without even the need to first convert your SAS datetime to a date value.

data test;
  have='31JAN2017:00:00:00'dt;
  format have datetime23.;
  want=put(have,e8601dn.);
run;

Capture.JPG

 

PROC Star
Posts: 633

Re: How to display Dates in Character Format?

What if the value of 'have' is 2017-03-31 in numeric format and I just to
need to convert it to character? Can I use the put function with the
format $10.?
Super User
Posts: 10,569

Re: How to display Dates in Character Format?

Well, 2017-03-31 gives an interesting numeric value:

data test;
x1 = 2017-03-31;
run;

proc print data=test;
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Solution
‎04-20-2018 06:14 AM
Respected Advisor
Posts: 4,791

Re: How to display Dates in Character Format?

[ Edited ]

@Babloo wrote:
What if the value of 'have' is 2017-03-31 in numeric format and I just to
need to convert it to character? Can I use the put function with the
format $10.?

@Babloo

I would have thought you understand in-between how SAS Date and DateTime values work. If the value is numeric then it's either the count of days (date value) or seconds (datetime value) since 1/1/1960. You then apply a format to make this value human readable as a date/datetime value. This doesn't change the value itself which just remains a number, the format is just for printing.

If you really want to store such a numeric value representing a data/datetime in a character variable, then you have to use the put() function. This function does nothing else than applying the format you use to your numeric value and then write the result as a string - and if this is on the right side from the equal sign then you're assigning this string value to a character variable. In other languages the comparable function to a SAS put() is called write()  - and read() for input().

 

Can I use the put function with the format $10.?

I hope you understand now that the answer to this question is "no, of course not". You have to use the appropriate date/datetime format to write the number as a date/datetime string.

 

And last but not least: It's really MUCH better to keep dates as SAS date/datetime values (=numeric) with a format permanently assigned to the variable for printing/looking at it. Keeping the dates numeric allows you to actually use such data (i.e. if you want to calculated the number of days between two dates and the like; or even if you just want to sort by date). 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 12 replies
  • 166 views
  • 3 likes
  • 4 in conversation