DATA Step, Macro, Functions and more

Reading dataset using input - incorrect results

Reply
Super Contributor
Posts: 272

Reading dataset using input - incorrect results

[ Edited ]

Dear sir,

 

I am trying to read the data using the following code. I am not getting the output as shown in datalines statement. Only first observation I am getting exactly. For next observations all values are "Y". Please help.

 

Thank you.

 

data one;
input AESER $1 +1 AESDTH $1 +1 AESLIFE $1 +1 AESHOSP $1 +1 AESCONG $1 +1 AESDISAB $1 +1 AESMIE $1 +1 INFECDIS $1;
datalines;
N N N N N N N N
Y N N N N N N N
Y Y N N N N Y N
Y Y Y N N N N N
;
run;

Frequent Contributor
Posts: 144

Re: Reading dataset using input - incorrect results

Posted in reply to knveraraju91

You have different alternative that work fine

data one;
input AESER $ @2 AESDTH $ @4 AESLIFE $ @6 AESHOSP $ @8 AESCONG $ @10 AESDISAB $ @12 AESMIE $ @14 INFECDIS $;
datalines;
N N N N N N N N
Y N N N N N N N
Y Y N N N N Y N
Y Y Y N N N N N
;
run;

data one;
input AESER $  AESDTH $  AESLIFE $  AESHOSP $  AESCONG $  AESDISAB $  AESMIE $  INFECDIS $;
datalines;
N N N N N N N N
Y N N N N N N N
Y Y N N N N Y N
Y Y Y N N N N N
;
run;

You don't need the +1 that mean skip it a column. 

Super User
Posts: 5,509

Re: Reading dataset using input - incorrect results

Posted in reply to knveraraju91

WIthin an INPUT statement:

 

$1 = read the contents of column 1

$1. = read the next column

 

Assuming you have one space between data values, you would need to change all occurrences of "$1" to "$1.".

Trusted Advisor
Posts: 1,117

Re: Reading dataset using input - incorrect results

Posted in reply to knveraraju91

Hello @knveraraju91,

 

While I was writing my post, @Astounding has already given the explanation ...

 

The explanation why your code failed is: You used column input inadvertently. SAS interpreted the specification "$1" as "read character value from column 1." Hence, all of your variables received the first letter in each line, the other letters were ignored. So, it only seemed that the first line was read correctly, because all letters happened to be equal to the first one.

 

What you probably wanted to do was formatted input, but informat specifications such as $1. require a period. The following INPUT statement is correct:

input AESER $1. +1 AESDTH $1. +1 AESLIFE $1. +1 AESHOSP $1. +1 AESCONG $1. +1 AESDISAB $1. +1 AESMIE $1. +1 INFECDIS $1.;

Now the "+1" pointer controls make sense, because with formatted input the blanks between the values are not skipped automatically.

 

The input style which skips blanks automatically (but which also requires blanks or other delimiters between data values) is list input. This in turn can be combined with informat specifications (which in your example have the main purpose of assigning type character and length 1 to the variables) and is then called modified list input:

input AESER :$1. AESDTH :$1. AESLIFE :$1. AESHOSP :$1. AESCONG :$1. AESDISAB :$1. AESMIE :$1. INFECDIS :$1.;

All of these repetitive specifications can be abbreviated by using parenthesized lists of variable names and informat specifications. In particular, modified list input could be written as:

input (AESER AESDTH AESLIFE AESHOSP AESCONG AESDISAB AESMIE INFECDIS) (:$1.);
Ask a Question
Discussion stats
  • 3 replies
  • 243 views
  • 3 likes
  • 4 in conversation