BookmarkSubscribeRSS Feed
ijm_wf
Fluorite | Level 6

I have a dataset with ten variables, VAR1-VAR10. Four of the variables are numeric dates and have a MMDDYYD10. format and the other six are character and have a $1000. format. My objective is to convert all ten of the variables to character with a $1000. format. It is necessary, however, that the dates maintain their originally formatting after being converted to character.  For example if a date displays as 10-11-2019 before the conversion, it must display as 10-11-2019 after the conversion.

 

What is the best way to convert all variables at once to character $1000.? Please provide code examples.

 

Thanks.

16 REPLIES 16
PaigeMiller
Diamond | Level 26

@ijm_wf wrote:

I have a dataset with ten variables, VAR1-VAR10. Four of the variables are numeric dates and have a MMDDYYD10. format and the other six are character and have a $1000. format. My objective is to convert all ten of the variables to character with a $1000. format. It is necessary, however, that the dates maintain their originally formatting after being converted to character.  For example if a date displays as 10-11-2019 before the conversion, it must display as 10-11-2019 after the conversion.

 

What is the best way to convert all variables at once to character $1000.? Please provide code examples.

First, you can't convert numeric variables to character, this is not possible. You can create new character variables of the desired length, that look identical to the formatted numeric variable, but why bother? It seems like unnecessary work. Why do you need numeric variables as character with format $1000.?

--
Paige Miller
ijm_wf
Fluorite | Level 6

Paige, what is the recommended way to "create new character variables of the desired length, that look identical to the formatted numeric variable"?

 

PaigeMiller
Diamond | Level 26

I believe @Kurt_Bremser answered this. The recommended way to do this conversion so you have character variables that appear to be identical to the formatted numeric variables is NOT doing it at all.

--
Paige Miller
Kurt_Bremser
Super User

The best way is to not do that. Character variables with a defined length of 1000 waste 996 (!) bytes when dates are stored there (numeric variables containing dates can be defined with a length of just 4), and you lose all the fine methods (functions, calculations, formats) of dealing with SAS date values.

Don't waste your time on something stupid.

Reeza
Super User

1. Query SASHELP.VCOLUMN or PROC CONTENTS to get the list of variables and types, and number of variables

2. Use VVALUEX to convert to formatted character value

3. Drop old variables.

 

Rough sketch of what that may look like. 

 

data want;

set have;

array _haveN(*) _numeric_;
array _haveC(*) _character_;

array newVar(*) $100. newVar1-newVar10;

do i= 1 to dim(_haveN);
newVar(i) = vvaluex(vname(_haveN(i)));
end;

do j=i to dim(_haveC);
newVar(j) = vvaluex(vname(_haveC(i)));
end;

run;

@ijm_wf wrote:

I have a dataset with ten variables, VAR1-VAR10. Four of the variables are numeric dates and have a MMDDYYD10. format and the other six are character and have a $1000. format. My objective is to convert all ten of the variables to character with a $1000. format. It is necessary, however, that the dates maintain their originally formatting after being converted to character.  For example if a date displays as 10-11-2019 before the conversion, it must display as 10-11-2019 after the conversion.

 

What is the best way to convert all variables at once to character $1000.? Please provide code examples.

 

Thanks.


 

Ksharp
Super User

@Reeza  Why not use VVALUE() which convert variable into character variable with its format .

Reeza
Super User

@Ksharp  VVALUE requires a literal name, VVALUEX takes an expression/formula, such as the result from VNAME().

 


@Ksharp wrote:

@Reeza  Why not use VVALUE() which convert variable into character variable with its format .


 

data_null__
Jade | Level 19

@Reeza wrote:

@Ksharp  VVALUE requires a literal name, VVALUEX takes an expression/formula, such as the result from VNAME().

 


@Ksharp wrote:

@Reeza  Why not use VVALUE() which convert variable into character variable with its format .


 


I think @Ksharp is saying you could have done it more directly using an array reference in VVALUE.

 

   vvalue(_haveC[I]);
Reeza
Super User
I don't recommend this either by the way and in 15 years don't think I've ever needed this. I've done similar things partially, creating data sets that have the raw value and the formatted value but that's usually when I need to transfer it to someone else who may not have SAS or to explore in Excel.
data_null__
Jade | Level 19

It is easy with PROC TRANSPOSE and there are valid reasons for doing.  Notice variable names and labels remain the same.

 

 

 

data cars;
   _obs_ + 1;
   set sashelp.cars;
   run;
proc transpose data=cars out=flip;
   by _obs_;
   var _all_;
   run;
data flip;
   set flip;
   where upcase(_name_) ne '_OBS_';
   col1 = left(col1);
   run;
proc transpose data=flip out=flop(drop=_name_);
   by _obs_;
   var col1;
   run;
proc contents varnum;
proc print;
   run;

Capture.PNGCapture.PNG

ijm_wf
Fluorite | Level 6

Please note that after the conversions, the final SAS dataset containing all character variables with $1000. format will be appended to a Teradata table with corresponding varchar(1000) fields.

Kurt_Bremser
Super User

@ijm_wf wrote:

Please note that after the conversions, the final SAS dataset containing all character variables with $1000. format will be appended to a Teradata table with corresponding varchar(1000) fields.


Wouldn't that also need a transpose, as such structures are usually used in name/value tables?

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
  • 16 replies
  • 5239 views
  • 7 likes
  • 7 in conversation