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

Hello! I am new to sas.

 

I am trying to read in a character decimal (oldvar = 0.599, ) . and create a  new numeric variable (want=0.599). 

 

I am unsure which format to use to read the data.

 

below is sample code I was trying to use, but wouldn't this create character value?

 

put(input(charvar,best4.),z4.);

 

This seems to round my decimals up:

 

input(charvar,best4.);

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Your first statement is creating a CHARACTER variable not a NUMERIC variable.  The PUT() function converts values to strings and the INPUT function converts strings to values.

 

Your second statement is closer, but the width used in your informat is too short.  '0.599' is 5 characters long.  Note there is not any reason to limit the width you use unless the string has trailing non digits that you want to ignore.  

Also note that there is no "best" informat.  If you use BEST as an informat that is just an alias for the normal numeric informat. Also do NOT specify a decimal part on your informat specification unless your character strings have purposely not included the period for the decimal point and you want SAS to insert one at the appropriate place.

 

data want;
  set have;
  numvar = input(charvar, 32.);
run;

 

If the value is PRINTING as an integer then you probably mistaken attached the wrong FORMAT to it.   Perhaps something like the Z4. format in your first example.  That is requesting that the value be rounded to an integer and displayed with exactly four digits even when the values is less than 1,000.   

 

There is no need to attach a format to the value as SAS already knows how to print numbers.   By default it will use the BEST12. format to display the numbers.  If your values are always less than 10 and non-negative and you want to display exact three digits to the right of the decimal point then you could use the 5.3 format (which can also be written as F5.3) to display one digit to the left of the decimal point and three to the right.

 

Also remember that binary floating point numbers cannot exactly represent all decimal fractions. 

View solution in original post

4 REPLIES 4
Astounding
PROC Star

This should do it:

 

newvar = input(oldvar, 5.);

 

You need a width of 5, not 4, because the width needs to include all characters to read (including the decimal point).

GypsyElder
Calcite | Level 5

Thank you for the quick response.

 

This works; however, it rounds. For example  0.599 is now 1.

Tom
Super User Tom
Super User

Your first statement is creating a CHARACTER variable not a NUMERIC variable.  The PUT() function converts values to strings and the INPUT function converts strings to values.

 

Your second statement is closer, but the width used in your informat is too short.  '0.599' is 5 characters long.  Note there is not any reason to limit the width you use unless the string has trailing non digits that you want to ignore.  

Also note that there is no "best" informat.  If you use BEST as an informat that is just an alias for the normal numeric informat. Also do NOT specify a decimal part on your informat specification unless your character strings have purposely not included the period for the decimal point and you want SAS to insert one at the appropriate place.

 

data want;
  set have;
  numvar = input(charvar, 32.);
run;

 

If the value is PRINTING as an integer then you probably mistaken attached the wrong FORMAT to it.   Perhaps something like the Z4. format in your first example.  That is requesting that the value be rounded to an integer and displayed with exactly four digits even when the values is less than 1,000.   

 

There is no need to attach a format to the value as SAS already knows how to print numbers.   By default it will use the BEST12. format to display the numbers.  If your values are always less than 10 and non-negative and you want to display exact three digits to the right of the decimal point then you could use the 5.3 format (which can also be written as F5.3) to display one digit to the left of the decimal point and three to the right.

 

Also remember that binary floating point numbers cannot exactly represent all decimal fractions. 

GypsyElder
Calcite | Level 5

Thank you! Great info! This is extremely helpful. 

 

Earlier in my code I accidently set missing to " "  instead of  .  so the value was returning as character and I didn't notice. 

 

 

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
  • 4 replies
  • 4624 views
  • 0 likes
  • 3 in conversation