- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.);
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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).
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thank you for the quick response.
This works; however, it rounds. For example 0.599 is now 1.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.