Discover More Tips and Techniques on This Blog

tod8. and picture formats: How Can I Find a Time Format that Does Not Remove the Leading Zero e.g. Time5

How Can I Find a Time Format that Does Not Remove the Leading Zero e.g. Time5
(output printed as 09:36 instead of 9:36)?
I have found an intersting solution to this problem in http://www.sas.com/; direct link

(Suggested by Stephen Hoar, Lloyds TSB)

There is currently no time format that puts leading zeros automatically in time values. But there are a number of ways of achieving this, below are some examples.

Example 1:
Create your own custom format using the following syntax. Then format your data values using the user defined format.


proc format ;
picture military other = '%0H:%0M:%0S' (datatype=time) ;
run ;

data test;
x= '9:36't ;
format x military8. ;
put x=;
run ;

x=09:36:00


Example 2:

You can also use the TODw.d format which does write leading zeros, but the original value must be a SAS datetime value. To convert your time values to datetime values use the DHMS function. In the DHMS function insert the value: 0 date, 0 hours, 0 minutes, and then the SAS time value as the number of seconds.

Then format the new variable with the TODw.d format and you will have the correct time, including the leading zeros.


data test;
x='9:36't;
y=dhms(0,0,0,x);
format y tod8.;
put y=;
run;

y=09:36:00

Have a look at the following Technical Support website for further examples of using Date & Time functions and formats: http://support.sas.com/techsup/sample/functions.html

SAS Arrays and DO-LOOPS

SAS Do Array

Three SAS Programs that use Arrays

SAS Macros that Convert a Directory of Transport Files

There is a set of SAS macros, converts a directory of transport files to a directory of SAS data sets and format catalogs (and vice versa). To see how to invoke the macros, look at the test following the last macro. The macros make the assumption that transport files created from data sets have the extension .xpt, and transport files created from format catalogs have the extension .xpf.
%expfmts : This macro will convert an existing format catalog into a data set in transport format.
%expdset: This macro will convert an existing SAS data set into a transport file.
%impfmts: This macro will convert a transport CNTLOUT data set into a native format catalog.
%impdset: This macro will convert a transport data set into a native SAS data set.
%getnames: This macro will create a SAS data set consisting of a variable called FILENAME. There will be one observation for each file in the specified directory with the specified extension.

Disclosure:

In the spirit of transparency and innovation, I want to share that some of the content on this blog is generated with the assistance of ChatGPT, an AI language model developed by OpenAI. While I use this tool to help brainstorm ideas and draft content, every post is carefully reviewed, edited, and personalized by me to ensure it aligns with my voice, values, and the needs of my readers. My goal is to provide you with accurate, valuable, and engaging content, and I believe that using AI as a creative aid helps achieve that. If you have any questions or feedback about this approach, feel free to reach out. Your trust and satisfaction are my top priorities.