Databases
--
Questions
--
Followers
Top Experts
SAS data and time stamp
Hi all,
I need to create a macro variable with date and time format.
I have a SAS dataset with date on it.
date
2011-11-04
I need to create a new macro variable by appending the current time with this date as
2011-11-04 04:15:00
Can anyone please help me. Thanks in advance.
I need to create a macro variable with date and time format.
I have a SAS dataset with date on it.
date
2011-11-04
I need to create a new macro variable by appending the current time with this date as
2011-11-04 04:15:00
Can anyone please help me. Thanks in advance.
Zero AI Policy
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
ASKER CERTIFIED SOLUTION
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Hi
iowa's method will work well, and you shouldn't need anything further, but here are a few macro assignments that can help:
%let dt = %SYSFUNC(DATETIME(),DATETI ME.); /* Current date and time */
%let time = %SYSFUNC(TIME(),TIME.); /* Current time */
%let date = %SYSFUNC(TODAY(),DATE9.); /*Current date */
%let yymm = %SYSFUNC(TODAY(), yymmn6.); Â /* Current month in format yyyymm*/
%let pyymm = %SYSFUNC(INTNX(MONTH, %SYSFUNC(TODAY(),-1,e), yymmn6.); Â /* Previous month in format yyyymm */
%let monthend = %SYSFUNC(INTNX(MONTH, %SYSFUNC(TODAY()), -1,e), DATE9.); Â /* Â Last day of last month */
Thus for your solution, you could use this assignment:
Either :
DATA Test;
 set test;
  NewDate = TRIM(DATE)||&time;
RUN;
OR
DATA Test;
 set test;
  NewDate = TRIM(DATE)||%SYSFUNC(TIME( ),TIME.);;
RUN;
OR
DATA Test;
 set test;
  NewDate = TRIM(DATE)||PUT(TIME(),TIM E.);
RUN;
Depending on what you need the timestamp to be;
iowa's method will work well, and you shouldn't need anything further, but here are a few macro assignments that can help:
%let dt = %SYSFUNC(DATETIME(),DATETI
%let time = %SYSFUNC(TIME(),TIME.); /* Current time */
%let date = %SYSFUNC(TODAY(),DATE9.); /*Current date */
%let yymm = %SYSFUNC(TODAY(), yymmn6.); Â /* Current month in format yyyymm*/
%let pyymm = %SYSFUNC(INTNX(MONTH, %SYSFUNC(TODAY(),-1,e), yymmn6.); Â /* Previous month in format yyyymm */
%let monthend = %SYSFUNC(INTNX(MONTH, %SYSFUNC(TODAY()), -1,e), DATE9.); Â /* Â Last day of last month */
Thus for your solution, you could use this assignment:
Either :
DATA Test;
 set test;
  NewDate = TRIM(DATE)||&time;
RUN;
OR
DATA Test;
 set test;
  NewDate = TRIM(DATE)||%SYSFUNC(TIME(
RUN;
OR
DATA Test;
 set test;
  NewDate = TRIM(DATE)||PUT(TIME(),TIM
RUN;
Depending on what you need the timestamp to be;
Hi,
I thought you might be looking for a more pure macro solution so here is something that might work for you.
-------------------------- ---------- ----------
* This just simulates getting the date value out of your dataset;
data _null_;
 ExistingDate = '01JAN1999'd;
 call symput('ExistingDate',Exis tingDate);
run;
*This will append the current time to the date date. Â I think this is the heart of what you needed;
* Notice that the DHMS function will except a time value in the last postiion as it is just the total seconds;
%let ExisitingDateWithTime = %sysfunc(dhms(&ExistingDat e,0,0,%sys func(time( ))));
* Here I am just converting it to a string so that we can output the results;
%let ExisitingDateWithTimeStr = %sysfunc(putn(&ExisitingDa teWithTime ,datetime4 0.));
%put &ExisitingDateWithTimeStr;
Hope that works.
Curtis
I thought you might be looking for a more pure macro solution so here is something that might work for you.
--------------------------
* This just simulates getting the date value out of your dataset;
data _null_;
 ExistingDate = '01JAN1999'd;
 call symput('ExistingDate',Exis
run;
*This will append the current time to the date date. Â I think this is the heart of what you needed;
* Notice that the DHMS function will except a time value in the last postiion as it is just the total seconds;
%let ExisitingDateWithTime = %sysfunc(dhms(&ExistingDat
* Here I am just converting it to a string so that we can output the results;
%let ExisitingDateWithTimeStr = %sysfunc(putn(&ExisitingDa
%put &ExisitingDateWithTimeStr;
Hope that works.
Curtis






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
Databases
--
Questions
--
Followers
Top Experts
Databases are organized collections of data, most commonly accessed through management systems including schemas, tables, queries and processes that allow users to enter and manipulate the information or utilize it in other fashions, such as with web applications or for reporting purposes.