BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
heyyou1
Fluorite | Level 6

I am reading in data from a excel sheet I cannot edit. SAS reads in the columns as CHAR and in order to do some calculations with them, I need to turn them into numeric. I tried a SQL modify statement,

/* Changing the columns from characters to numeric */
proc sql;
   alter table race_sjc_mph_data
      modify first_dose_administered format=comma15.;
quit;

but this results in this error.

      
 
 151        
 152        /* Changing the columns from characters to numeric */
 153        proc sql;
 154           alter table race_sjc_mph_data
 155              modify first_dose_administered format=comma15.;
 ERROR: Character column first_dose_administered requires a character format specification.
 NOTE: Table WORK.RACE_SJC_MPH_DATA has been modified, with 17 columns.
 156        quit;
 NOTE: PROCEDURE SQL used (Total process time):
       real time           0.00 seconds
       cpu time            0.00 seconds
       
 

This is the current output properties of that table

heyyou1_0-1637333211819.png

From what I see in the data, there is not stray characters that would cause an error in parsing from character to numeric.

 

Thanks you

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

You cannot change the TYPE of an existing variable (changing the format attached to a variable just changes how you want the variable displayed).  So make a new numeric variable.  

 

Does the value in the character variable include commas? Is that why you wanted to display the number with the comma format?  Try using the COMMA informat when converting the values.  That will ignore the commas and dollar signs in the character string when converting the string into a number.

data want;
  set race_sjc_mph_data;
  dose1 = input(first_dose_administered,comma32.);
run;

If you would prefer the use the existing name for the numeric variable then use RENAME statement to change the names.

data want;
  set race_sjc_mph_data;
  dose1 = input(first_dose_administered,comma32.);
  rename first_dose_administered=dose1_char dose1=first_dose_administered;
run;

 

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26

You cannot change a character variable to a numeric variable. Not a feature of SAS.

 

What you can do is create a new variable that is the numeric equivalent of the character variable, for example in a data step:

 

first_dose_administered_num = input(first_dose_administered,8.);

This has nothing to do with formats, which are irrelevant to this type of conversion.

--
Paige Miller
Tom
Super User Tom
Super User

You cannot change the TYPE of an existing variable (changing the format attached to a variable just changes how you want the variable displayed).  So make a new numeric variable.  

 

Does the value in the character variable include commas? Is that why you wanted to display the number with the comma format?  Try using the COMMA informat when converting the values.  That will ignore the commas and dollar signs in the character string when converting the string into a number.

data want;
  set race_sjc_mph_data;
  dose1 = input(first_dose_administered,comma32.);
run;

If you would prefer the use the existing name for the numeric variable then use RENAME statement to change the names.

data want;
  set race_sjc_mph_data;
  dose1 = input(first_dose_administered,comma32.);
  rename first_dose_administered=dose1_char dose1=first_dose_administered;
run;

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 10274 views
  • 0 likes
  • 3 in conversation