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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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