DATA Step, Macro, Functions and more

How does formatted input work?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 13
Accepted Solution

How does formatted input work?

How does the variable weight has the value 2 in the following example?

Example :

data test;

  input @1 height 2. @4 weight 2;

datalines;

72 95

;

Output:

Height    Weight

   72           2

Is there any special usage of formatted input when there is no period in the numeric informat ?


Accepted Solutions
Solution
‎04-03-2014 04:26 AM
Super User
Posts: 6,938

Re: How does formatted input work?

Since "1" or "2" is not a format, but a value, and follows a variable name, it is interpreted by the input statement as a column. Therefore you read the contents of columns 2 and 1, respectively, which happen to be 2 and 7.

An "informat" without a dot is not an informat, it is ALWAYS something else.

The only time you write a format/informat name without a dot is when specifying it in proc format at creation time.

Edited a typo

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Super User
Posts: 6,938

Re: How does formatted input work?

"2" is not a format, it is a value. "2." is a format. A format MUST be called with a dot.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 13

Re: How does formatted input work?

Kurt,

It is correct that we must use informat with a dot/period.

But I need to know how SAS works behind the scenes when the informat is without a dot or how the column pointer moves in that case.

As an alternative to the above example, if I use 1 instead of 2 then the output value for  variable weight is 7.

Example :

data test;

  input @1 height 2. @4 weight 1;

datalines;

72 95

;

Output:

Height    Weight

   72           7

Solution
‎04-03-2014 04:26 AM
Super User
Posts: 6,938

Re: How does formatted input work?

Since "1" or "2" is not a format, but a value, and follows a variable name, it is interpreted by the input statement as a column. Therefore you read the contents of columns 2 and 1, respectively, which happen to be 2 and 7.

An "informat" without a dot is not an informat, it is ALWAYS something else.

The only time you write a format/informat name without a dot is when specifying it in proc format at creation time.

Edited a typo

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 13

Re: How does formatted input work?

Thanks Kurt, I got the point that it reads at the value particular column.

Also, I am curious to know why SAS does this unexpected thing and also doesn't show any NOTE in SAS log.

Super User
Posts: 6,938

Re: How does formatted input work?

It is not unexpected behaviour, just see the SAS documentation for the input statement:

SAS(R) 9.2 Language Reference: Dictionary, Fourth Edition

Go to "Input Styles - Column Input".

Since it can do what the code told it to do without problems, SAS sees no need for NOTE, WARNING or ERROR messages.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 13

Re: How does formatted input work?

Thanks Kurt.

I completely misjudged it and didn't think it that it was a 'Column Input'.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 452 views
  • 3 likes
  • 2 in conversation