Link to home
Create AccountLog in
Databases

Databases

--

Questions

--

Followers

Top Experts

Avatar of aruku
aruku

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.

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
Avatar of Aloysius LowAloysius Low🇸🇬

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

Avatar of theartfuldazzlertheartfuldazzler🇿🇦

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(),DATETIME.); /* 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(),TIME.);
RUN;


Depending on what you need the timestamp to be;



Avatar of mackcemackce🇺🇸

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',ExistingDate);
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(&ExistingDate,0,0,%sysfunc(time())));
* Here I am just converting it to a string so that we can output the results;
%let ExisitingDateWithTimeStr = %sysfunc(putn(&ExisitingDateWithTime,datetime40.));
%put &ExisitingDateWithTimeStr;

Hope that works.
Curtis

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.

Databases

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.