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

Hi team,

 

I have requirement to create a datetime variable consisting of different timestamp when run each time using a date constant field.

rundate='02JUN2022'd;

data _null_;
rut=dhms(&rundate,hour(&rundate),minute(&rundate),second(&rundate));
call symput("ruts",rut);
run;

%put &ruts.;

%let timest=%sysfunc(putn(&ruts,B8601DN8))%sysfunc(timepart(&ruts),B8601TM6);

%put &timest.;

 

when I run this query it create same timest value. I need to have different time value( hMS) when we run this query.

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

@sddxt wrote:
I want the above query give me different minute and second value each run.


The obvious way to do this is to use the system clock — but you don't actually say that's what you want, and you still need to be more specific about how to obtain different hour minute second each time — well you don't even say you want different hours in each run.

 

data _null_;
    rundate='02JUN2022'd;
    t=time();
    rut=dhms(rundate,hour(t),minute(t),second(t));
    call symput("ruts",rut);
run;

%put &ruts;
%put %sysfunc(putn(&ruts,datetime16.));

 

--
Paige Miller

View solution in original post

7 REPLIES 7
Tom
Super User Tom
Super User

I do not understand what it is that you are trying to do?

Where are going to get a value for HOURS, MINUTES or SECONDS from a value that is only in DAYS?

 

Do you just want to add some random number of seconds?

sddxt
Calcite | Level 5
when i run the above query i get 20220602061958 in timest everytime I run the above query. I want the above query give me different minute and second value each run.

Example: 20220602061958 last 4 digits should be different each time we run this.
PaigeMiller
Diamond | Level 26

@sddxt wrote:
I want the above query give me different minute and second value each run.


The obvious way to do this is to use the system clock — but you don't actually say that's what you want, and you still need to be more specific about how to obtain different hour minute second each time — well you don't even say you want different hours in each run.

 

data _null_;
    rundate='02JUN2022'd;
    t=time();
    rut=dhms(rundate,hour(t),minute(t),second(t));
    call symput("ruts",rut);
run;

%put &ruts;
%put %sysfunc(putn(&ruts,datetime16.));

 

--
Paige Miller
ballardw
Super User

What your code is doing is treating the numeric value of &rundate, which is a number of days, as time values with the hour, minute and second functions. Time values are numbers of seconds. So it is improper to really expect any useful result from that particular DHMS call.

 

Now, provide what time of day you expect the result to be.

sddxt
Calcite | Level 5
Yes you are correct. So I need to have value generated which is combination of rundate
20220602 and randon hhMMSS combined together.

which will give me value in timest as big number as 20220602061958 where HHMMSS change value with everyrun.
PaigeMiller
Diamond | Level 26

@sddxt wrote:
Yes you are correct. So I need to have value generated which is combination of rundate
20220602 and randon hhMMSS combined together.

which will give me value in timest as big number as 20220602061958 where HHMMSS change value with everyrun.

 

 


okay, random times

 

 

data _null_;
    rundate='02JUN2022'd;
    rut=86400*rundate+rand('uniform',0,86400);
    call symput("ruts",rut);
run;

%put &ruts;
%put %sysfunc(putn(&ruts,datetime19.));

 

 

--
Paige Miller
ballardw
Super User

This generates a random time to add to the date.

data _null_;
t = int(rand('uniform','00:00:00't,'23:59:59't));
rut=dhms(&rundate,0,0,t);
call symput("ruts",rut);
run;
 
%put &ruts;

The INT function is to return integer portion as the rand function will return decimals.

 

NOTE: Any time you create macro variable from something numeric you need to take responsibility of the rules involved. If the values I used were not returning integers you could end up with a varying number of decimal places in that value.

Also you want to consider using SYMPUTX instead of SYMPUT.

 

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!

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
  • 7 replies
  • 1120 views
  • 0 likes
  • 4 in conversation