BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Aidaan_10
Calcite | Level 5

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,

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
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,


 

 

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

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?

--
Paige Miller
Reeza
Super User
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,


 

 

Tom
Super User Tom
Super User

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.

Aidaan_10
Calcite | Level 5

new_var=vvalue(old_var) worked very well. Thank you so much

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
  • 5 replies
  • 4840 views
  • 1 like
  • 5 in conversation