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: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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