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 ×t.;
when I run this query it create same timest value. I need to have different time value( hMS) when we run this query.
@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.));
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 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.));
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 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.));
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 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.