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

I am trying to create a file from SAS with date and timestamp on as below,

NAME_201907241615A.TXT

 

I could able to get the date in YYYYMMDD format using date() function with YYMMDDN8 format.

But i am finding difficult to get the time in 24 hr format. Or else if time is 9 AM , space is coming before 9 instead of 09.

 

So i tried this logic and it is working good as expected,

 

%let Time = %sysfunc(time(),time.) ;
%let Time_HH = %scan(&Time,1,:) ;
%let Time_MM = %scan(&Time,2,:) ;

HH=PUT(&TIME_HH,2.);
if length(hh)<3 and '0'>= hh < '10' then hh=repeat('0',2-length(hh))||strip(hh); 
MM =PUT(&TIME_MM,2.);
if length(mm)<3 and '0'>= mm < '10' then mm=repeat('0',2-length(mm))||strip(mm);

var9="EDS"||"_"||"&VEN"||"_"||"&LOB"||"_"||"&TYP"||TRIM("_")||strip(%sysfunc(date(),YYMMDDN8.))||TRIM(HH)||TRIM(MM)||TRIM("A")||&VER;

 

The red highlighted portion i tried to get hour and minute as 2 digit (like 09), if the time is single digit.

I think there could be a straightforward approach , which i am missing. Is there is any other options to do this simply?Can we do this using picture clause in format?

 

Thanks,

Meenakshi

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
%let fileTimeStamp = %sysfunc(date(), yymmddn8.)%sysfunc(putc(%sysfunc(time(), b8601TM6.), $4.)) ;
%put &fileTimeStamp.;
Results:
71 %let fileTime = %sysfunc(date(), yymmddn8.)%sysfunc(putc(%sysfunc(time(), b8601TM6.), $4.)) ;
72 %put &fileTime.;
201907310938
YYYYMMDDHHMM
 

@meenakshim wrote:

I am trying to create a file from SAS with date and timestamp on as below,

NAME_201907241615A.TXT

 

I could able to get the date in YYYYMMDD format using date() function with YYMMDDN8 format.

But i am finding difficult to get the time in 24 hr format. Or else if time is 9 AM , space is coming before 9 instead of 09.

 

So i tried this logic and it is working good as expected,

 

%let Time = %sysfunc(time(),time.) ;
%let Time_HH = %scan(&Time,1,:) ;
%let Time_MM = %scan(&Time,2,:) ;

HH=PUT(&TIME_HH,2.);
if length(hh)<3 and '0'>= hh < '10' then hh=repeat('0',2-length(hh))||strip(hh); 
MM =PUT(&TIME_MM,2.);
if length(mm)<3 and '0'>= mm < '10' then mm=repeat('0',2-length(mm))||strip(mm);

var9="EDS"||"_"||"&VEN"||"_"||"&LOB"||"_"||"&TYP"||TRIM("_")||strip(%sysfunc(date(),YYMMDDN8.))||TRIM(HH)||TRIM(MM)||TRIM("A")||&VER;

 

The red highlighted portion i tried to get hour and minute as 2 digit (like 09), if the time is single digit.

I think there could be a straightforward approach , which i am missing. Is there is any other options to do this simply?Can we do this using picture clause in format?

 

Thanks,

Meenakshi



 
 

View solution in original post

8 REPLIES 8
Tom
Super User Tom
Super User

If you want to display numbers with leading zeros use the Z format.

880   data _null_;
881     x=5;
882     put x Z2.;
883   run;

05
Reeza
Super User
%let fileTimeStamp = %sysfunc(date(), yymmddn8.)%sysfunc(putc(%sysfunc(time(), b8601TM6.), $4.)) ;
%put &fileTimeStamp.;
Results:
71 %let fileTime = %sysfunc(date(), yymmddn8.)%sysfunc(putc(%sysfunc(time(), b8601TM6.), $4.)) ;
72 %put &fileTime.;
201907310938
YYYYMMDDHHMM
 

@meenakshim wrote:

I am trying to create a file from SAS with date and timestamp on as below,

NAME_201907241615A.TXT

 

I could able to get the date in YYYYMMDD format using date() function with YYMMDDN8 format.

But i am finding difficult to get the time in 24 hr format. Or else if time is 9 AM , space is coming before 9 instead of 09.

 

So i tried this logic and it is working good as expected,

 

%let Time = %sysfunc(time(),time.) ;
%let Time_HH = %scan(&Time,1,:) ;
%let Time_MM = %scan(&Time,2,:) ;

HH=PUT(&TIME_HH,2.);
if length(hh)<3 and '0'>= hh < '10' then hh=repeat('0',2-length(hh))||strip(hh); 
MM =PUT(&TIME_MM,2.);
if length(mm)<3 and '0'>= mm < '10' then mm=repeat('0',2-length(mm))||strip(mm);

var9="EDS"||"_"||"&VEN"||"_"||"&LOB"||"_"||"&TYP"||TRIM("_")||strip(%sysfunc(date(),YYMMDDN8.))||TRIM(HH)||TRIM(MM)||TRIM("A")||&VER;

 

The red highlighted portion i tried to get hour and minute as 2 digit (like 09), if the time is single digit.

I think there could be a straightforward approach , which i am missing. Is there is any other options to do this simply?Can we do this using picture clause in format?

 

Thanks,

Meenakshi



 
 
data_null__
Jade | Level 19
Why not make a picture format?
Reeza
Super User
Picture formats for datetimes aren't honoured in some graphics plot and/or SAS Viya for custom formats. So using out of the box options means it'll work out of the box in most versions of SAS. But picture format is definitely doable and a good approach. It's also more code - technically 🙂
data_null__
Jade | Level 19
What does graphics or Viya have to do with making a file name with a time stamp?
Reeza
Super User

Nothing, it's just a style of defensive programming that means that its easier to port programs. 

There are likely at least five different ways this could be solved, that's just one option that seems to align with what the user understand and wants.

meenakshim
Fluorite | Level 6

Thank u so much for your comment. Its working good. Can you give me a sample to do the same using picture format if possible??

Reeza
Super User

Here's a close option:

 

proc format; 
  picture  datetimeStamp
          low-high='%Y%0m%0d%0H%0M%p' (datatype=datetime);
run;

%put %sysfunc(datetime(), datetimeStamp.);

You can play around with the date directives to customize it if needed. 

Literals are documented here:

https://go.documentation.sas.com/?docsetId=proc&docsetTarget=p0n990vq8gxca6n1vnsracr6jp2c.htm&docset...

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 8 replies
  • 6326 views
  • 1 like
  • 4 in conversation