Hi,
I have a date variable with the value as '7/30/2019 14:43' and I want to convert it into a character variable keeping the same format how do I do this? Any help will be greatly appreciated.
Thanks,
new_var = putn(old_var, vformat(old_var));
new_var2 = vvalue(old_var);
Use PUTN() to convert it to a character variable.
Use VFORMAT() to get the format that was displaying the variable and apply it in the conversion.
or Use VVALUE to get the formatted value into a character.
EDIT: Modified as per @Tom suggestions
@Aidaan_10 wrote:
Hi,
I have a date variable with the value as '7/30/2019 14:43' and I want to convert it into a character variable keeping the same format how do I do this? Any help will be greatly appreciated.
Thanks,
Character variables cannot have the same format as numeric variables.
If you want the character variable to appear the same as the formatted numeric variable ... why? What is the benefit of changing the variable type from numeric to character if it appears the same to us humans?
Look which format is used to display this datetime variable and use that in a put() function.
new_var = putn(old_var, vformat(old_var));
new_var2 = vvalue(old_var);
Use PUTN() to convert it to a character variable.
Use VFORMAT() to get the format that was displaying the variable and apply it in the conversion.
or Use VVALUE to get the formatted value into a character.
EDIT: Modified as per @Tom suggestions
@Aidaan_10 wrote:
Hi,
I have a date variable with the value as '7/30/2019 14:43' and I want to convert it into a character variable keeping the same format how do I do this? Any help will be greatly appreciated.
Thanks,
Huh? If it is a DATETIME value then you would need PUTN() instead of PUTC() as the N/C refers in the data type that is being formatted. The output of applying a format is always character.
Note that you can use the VVALUE() function to see the formatted value of a variable without first having find out what format, if any, is attached to it.
new_var=vvalue(old_var) worked very well. Thank you so much
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.