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.
@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, what is the recommended way to "create new character variables of the desired length, that look identical to the formatted numeric variable"?
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.
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.
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.
@Reeza Why not use VVALUE() which convert variable into character variable with its format .
@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]);
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;
@data_null__ wrote:
... there are valid reasons for doing.
Since no reasons were given, I see it as my duty here to protect a newbie from doing something that is, ahem, not very efficient.
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.
@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 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.