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

Hi ,

 

Can you please explain me below code step by step


data _null_;
runtime = compress(cat((put(today(),YYMMDDn8.)),(put(hour(time()), z2.)),(put(minute(time()), z2.)),'00'));
call symput ('runtime',compress(trim(runtime)));
run;

 

Thanks

SK

1 ACCEPTED SOLUTION

Accepted Solutions
Amir
PROC Star

Hi,

 

For a step-by-step explanation, it might help you to see what is happening if you write the step out to a data set, as per the below code. The original data _null_ code simply means a data step will not be created from the data statement. 

 

data have;
  yymmdd  = put(today(),YYMMDDn8.);                /* #1 current year, month & date formatted to have no separators     */
  hour    = put(hour(time()), z2.);                /* #2 current hour formatted to have leading zeros to a width of 2   */
  minute  = put(minute(time()), z2.);              /* #3 current minute formatted to have leading zeros to a width of 2 */
  cat     = cat(yymmdd,hour,minute,'00');          /* #4 concatenate previously created values and add '00' to the end  */
  runtime = compress(cat);                         /* #5 remove any undesired characters                                */
  call symput ('runtime',compress(trim(runtime))); /* #6 save the value of runtime as a macro variable                  */
run;

 

The above code produces a data set as follows:

 

Amir_0-1697823903566.png

 

Your runtime value will likely be different, as it is dependant on when the code is run.

 

#1 uses the today() function which returns today's date, and the put() function is used to format how the value is displayed based on the given format, which in this case is YYMMDDn8., i.e., occupy a width of 8 with no separators.

 

#2 uses the time() function which returns the current time, then the hour() function to extract just the hour value, and then the put() function is used to format how the value is displayed based on the given format, which in this case is z2., i.e., occupy a width of 2 using a leading zero ('0') if required.

 

#3 uses the time() function which returns the current time, then the minute() function to extract just the minute value, and then the put() function is used to format how the value is displayed based on the given format, which in this case is z2., i.e., occupy a width of 2 using a leading zero ('0') if required.

 

#4 uses the cat() function to join all of the previously created strings together as well as '00' on the end.

 

#5 uses the compress() function to remove any undesired characters, but as there no characters specified to be removed then the default characters are removed - see further below for a note on the documentation.

 

#6 uses the call symput() routine to create a macro variable (first argument), with a value supplied in the second argument. In this case the macro variable has been given the same name as the program variable, i.e., "runtime". The use of compress() and trim() appear to be redundant here.

 

If you need further help with the functions used then you can consult the documentation for General Functions Syntax Listed Alphabetically.

 

HTH.

 

 

Thanks & kind regards,

Amir.

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

If you run it and then add

 

%put &=runtime;

 

you will see that it is creating a macro variable named RUNTIME whose value is the current date and time, with seconds forced to zero, and all special characters removed.

 

Are there better ways to do this? Probably, but I haven't bothered to try to figure out a better way, primarily because I don't believe macro variables should have formatted values like this in most cases, and also I believe that datetime values should be handled as numeric rather than character strings that have to be pulled apart and then joined.

--
Paige Miller
PaigeMiller
Diamond | Level 26

Okay I thought of a quick easy way to do this, that doesn't require the datetime value to be handled as a character string to be pulled apart and re-assembled. I still don't think in most cases macro variables should have formatted date/time values.

 

proc format; /* Create picture format that produces the exact format desired for date/time values *?
    picture special low-high='%Y%0m%0d%0h%0M00' (datatype=datetime) ;
run;
data _null_;
    call symputx('runtime',put(datetime(),special.));
run;
%put &=runtime;

 

--
Paige Miller
Quentin
Super User

It's creating a timestamp. 

 

Peter Crawford wrote a lovely paper about a tiny utility macro to create timestamps.  

https://support.sas.com/resources/papers/proceedings/proceedings/sugi31/038-31.pdf

 

If you change the default format to be closer to yours, it would be:

%MACRO now( fmt= b8601dt15 ) /DES= 'timestamp';
 %SYSFUNC( DATETIME(), &fmt )
%MEND now ;

Use like:

1    %MACRO now( fmt= b8601dt15 ) /DES= 'timestamp';
2     %SYSFUNC( DATETIME(), &fmt )
3    %MEND now ;
4
5    %put %now() ;
20231020T135021
6
7    %let runtime=%now() ;
8    %put &=runtime ;
RUNTIME=20231020T135021

Note the format is slightly different than yours.  It has  T to separate the date from the time, and includes the actual seconds.

 

It's handy to use when you want a timestamp for a footnote, or as part of an output file name, or whatever.

 

BASUG is hosting free webinars Next up: Don Henderson presenting on using hash functions (not hash tables!) to segment data on June 12. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
Amir
PROC Star

Hi,

 

For a step-by-step explanation, it might help you to see what is happening if you write the step out to a data set, as per the below code. The original data _null_ code simply means a data step will not be created from the data statement. 

 

data have;
  yymmdd  = put(today(),YYMMDDn8.);                /* #1 current year, month & date formatted to have no separators     */
  hour    = put(hour(time()), z2.);                /* #2 current hour formatted to have leading zeros to a width of 2   */
  minute  = put(minute(time()), z2.);              /* #3 current minute formatted to have leading zeros to a width of 2 */
  cat     = cat(yymmdd,hour,minute,'00');          /* #4 concatenate previously created values and add '00' to the end  */
  runtime = compress(cat);                         /* #5 remove any undesired characters                                */
  call symput ('runtime',compress(trim(runtime))); /* #6 save the value of runtime as a macro variable                  */
run;

 

The above code produces a data set as follows:

 

Amir_0-1697823903566.png

 

Your runtime value will likely be different, as it is dependant on when the code is run.

 

#1 uses the today() function which returns today's date, and the put() function is used to format how the value is displayed based on the given format, which in this case is YYMMDDn8., i.e., occupy a width of 8 with no separators.

 

#2 uses the time() function which returns the current time, then the hour() function to extract just the hour value, and then the put() function is used to format how the value is displayed based on the given format, which in this case is z2., i.e., occupy a width of 2 using a leading zero ('0') if required.

 

#3 uses the time() function which returns the current time, then the minute() function to extract just the minute value, and then the put() function is used to format how the value is displayed based on the given format, which in this case is z2., i.e., occupy a width of 2 using a leading zero ('0') if required.

 

#4 uses the cat() function to join all of the previously created strings together as well as '00' on the end.

 

#5 uses the compress() function to remove any undesired characters, but as there no characters specified to be removed then the default characters are removed - see further below for a note on the documentation.

 

#6 uses the call symput() routine to create a macro variable (first argument), with a value supplied in the second argument. In this case the macro variable has been given the same name as the program variable, i.e., "runtime". The use of compress() and trim() appear to be redundant here.

 

If you need further help with the functions used then you can consult the documentation for General Functions Syntax Listed Alphabetically.

 

HTH.

 

 

Thanks & kind regards,

Amir.

soujanyak
Fluorite | Level 6

Super explanation, thanks a lot  

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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
  • 5 replies
  • 713 views
  • 2 likes
  • 4 in conversation