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

Hello,

 

I have the following records from a proc import (excel):

VariableTypeLenFormatInformatValue
var1Char17$17.00$17.002106742.5
var2Char18$18.00$18.00670637.14

 

 

then i run the following code:


data y;
set x;
var3=input(var1, 8.);
var4=input(var2, 8.);

drop var1 var2;
;
run;

 

the results however loses decimal numbers.  When I use proc compare they are not equal.

VariableTypeLenFormatInformatValue
Var3Num8  2106742
Var4Num8  670637.1

 

please help.

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

You told SAS to use 8 characters in doing this conversion. SAS did what you told it to do. You obviously need a wider informat in the INPUT statement.

--
Paige Miller

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

You told SAS to use 8 characters in doing this conversion. SAS did what you told it to do. You obviously need a wider informat in the INPUT statement.

--
Paige Miller
jffeudo86
Quartz | Level 8
Isn't the "8." telling SAS I want to convert it to numeric with 8 bytes?
ballardw
Super User

@jffeudo86 wrote:
Isn't the "8." telling SAS I want to convert it to numeric with 8 bytes?

NO.

 

When using an informat with the INPUT function you are telling how many characters to read.

The "bytes" would come in a Length or Attrib statement. Numeric values will default to 8 bytes unless otherwise specified.

 

data example;
   x= '12345678';
   y1=input(x,1.);
   y2=input(x,2.);
   y3=input(x,3.);
   y4=input(x,4.);
   y5=input(x,5.);
   y6=input(x,6.);
   y7=input(x,7.);
   y8=input(x,8.);
   put _all_;
run;
Tom
Super User Tom
Super User

@jffeudo86 wrote:
Isn't the "8." telling SAS I want to convert it to numeric with 8 bytes?

Sounds like you are confused about SAS data types and what formats and informats are.

SAS has two data types. Fixed length character strings or floating point numbers.   When you create a variable it is one or the other.

 

You can use a FORMAT to convert a value to text and an INFORMAT to convert text to a value.  You use an INFORMAT with the INPUT statement or the INPUT() , INPUTN() or INPUTC() function. You use a FORMAT with the PUT statement or the PUT(), PUTN() or PUTC() function.  A numeric informat creates a number. A numeric format works on numbers.

 

The width of a format is how many bytes the resulting text takes. The width of in informat is how many bytes of text to read.

 

You can attach informats and formats to variable with the INFORMAT and FORMAT statements. That is only needed if you want to give SAS special instructions on how to read values from text or how to display values as text.  For normal numbers and strings there is no need to attach either to a variable since SAS does not need special instructions for how to read or write numbers or strings.  DATE, TIME and DATETIME values are the exception since you do need special instructions to convert the number of days or seconds into human recognizable text.

 

Note for your problem there is no reason not to use the maximum width, 32, that the numeric informat allows.  If the string being read is shorter than that it is fine.

 

jffeudo86
Quartz | Level 8
PS. I compare them to another dataset whose variables are numeric and values are the same as in the top table.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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