BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
kajal_30
Quartz | Level 8

I have a dataset 'have' and numeric variables 'a' , 'b' ,'c' , 'd' in it and I want to convert them all into character and assign format $3. keeping the variable names same 'a' , 'b' ,'c' , 'd' .

 

thanks

Kajal 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
data want;
set have (rename = (a = old_a b=old_b c=old_c));
 
array old(3) old_a old_b old_c;
array new(3) $3. a b c;

do i=1 to dim(old);
new(i) = put(old, $3. -l);
end;

drop old_a old_b old_c;
run;

untested, the rename data set option may not be exactly correct but this is the idea to do it in one step. 

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

Can't be done directly, you cannot change numeric variables to character variables.

 

You could create rename the numeric variables to some other name, then create new character variables with the names you want, assign the values of the numeric variables to the new character variables, then delete the numeric variables.

 

Usually it is not a good idea to turn numeric variables into character variables, you are better off leaving them as numeric in most applications. Leaving the variable as numeric is also the easiest approach. So, my advice is to just leave the variables as numeric, instead of making extra work for yourself that has little value.

--
Paige Miller
Tom
Super User Tom
Super User

@kajal_30

I have a dataset 'have' and numeric variables 'a' , 'b' ,'c' , 'd' in it and I want to convert them all into character and assign format $3. keeping the variable names same 'a' , 'b' ,'c' , 'd' .

 

thanks

Kajal 


There is usually no need to attach any FORMAT to a character variable.  Are you saying they should be of length $3 (so they can store strings up to 3 digits long)?

How do you want the numbers to be converted into strings?  Are the values all integers?  Are all of the values between -99 and 999?  Should values between 0 and 99 use leading zeros?  Leading spaces?  Or trailing spaces?

You will have to make NEW variables.  You can then DROP the old variables and RENAME to new variables to use the old names.

Here is code to convert them using the Z3. format so that the small values have leading zeros.

data want;
  set have;
   array old a b c d ;
   array new $3 x1-x4 ;
   do over old;
      new = put(old,Z3.);
   end;
   drop a b c d ;
   rename x1=a x2=b x3=c x4=d ;
run;

If you would rather they have leading spaces just use 3. format in the PUT() function.  If you want spaces at the end instead of the beginning use the -L suffix to the format specification in the PUT() function.

Reeza
Super User
data want;
set have (rename = (a = old_a b=old_b c=old_c));
 
array old(3) old_a old_b old_c;
array new(3) $3. a b c;

do i=1 to dim(old);
new(i) = put(old, $3. -l);
end;

drop old_a old_b old_c;
run;

untested, the rename data set option may not be exactly correct but this is the idea to do it in one step. 

PaigeMiller
Diamond | Level 26

@kajal_30 wrote:

I have a dataset 'have' and numeric variables 'a' , 'b' ,'c' , 'd' in it and I want to convert them all into character and assign format $3. keeping the variable names same 'a' , 'b' ,'c' , 'd' .

 

thanks

Kajal 


Maybe what you need is a custom format to make the numeric values appear as character.

 

For example:

 

proc format;
    value cust 0='CAT' 1='DOG' 2='PIG';
run;
data want; 
    set have;
    format a b c d cust.;
run;
--
Paige Miller

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
  • 2109 views
  • 4 likes
  • 4 in conversation