BookmarkSubscribeRSS Feed
MikeXue
Calcite | Level 5

Hello All, 

 

I'm trying to create a new variable to set to another datetime variable. 

 

Data End_Date_Time ;
set IN_BOTH;
A_ETM_&ADAMEAETIME. = A_&ADAMEAETIME.;
run;

 

The original variable A_&ADAMEAETIME has the following value format: 

MikeXue_0-1638649266540.png

 

However, my new variable A_ETM_&ADAMEAETIME as the following format: 

MikeXue_1-1638649307686.png

 

What do I do need to in order to keep the formatting of the original variable in my new variable? Also, I do not want to rename the variable due to circumstances of the data and overlapping that can occur. So creating a new variable is a must. 

 

Thanks for your help in advance!

Mike

3 REPLIES 3
Patrick
Opal | Level 21

The easiest way is to just define a format for the new variable. In below code that's option 1.

If you want to create the new variable using exactly the attributes of another variable without having to define them explicitly then option 2 provides an approach for this.

data have;
  a_etm=datetime();
  format a_etm datetime.;
run;

/* option 1 */
data want1;
  set have;
  format new_var datetime.;
  new_var=mapping;
run;

/* option 2 */
data mapping;
  stop;
  set have(keep=a_etm);
  rename a_etm=new_var;
run;

data want;
  set have;
  if 0 then set mapping;
  new_var=a_etm;
run;
PaigeMiller
Diamond | Level 26

You don't need to create a new SAS data set for this. Just put the FORMAT statement in the same data step that creates the variable.

 

Or if the dataset containing this new variable (unformatted) already exists, just modify the metadata by using PROC DATASETS so that this new variable has the desired format.

 

proc datasets library=work nolist;
    modify End_Date_Time;
    format A_ETM_&ADAMEAETIME datetime16.;
run; quit;
--
Paige Miller

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