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:
However, my new variable A_ETM_&ADAMEAETIME as the following format:
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
Use a FORMAT or ATTRIB statement to assign the wanted format to the new variable.
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;
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;
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.