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