Datalines/Cards Keywords

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 122
Accepted Solution

Datalines/Cards Keywords

I have been using the following code, but I keep getting an Invalid data error, which shows me that the the entire array below is being read as one character variable. What do I need to do to make the datastep run properly?

 
data med;

   input location $2 hpa1-hpa15;

   cards;

   US -17.63753 -4.98 -3.21 -2.10 2.83 3.14 3.26 3.29 3.30 3.30 3.30 3.30 3.30 3.30 3.30
   AK -14.09531 -2.62 -1.92 -1.47 3.10 3.23 3.28 3.30 3.30 3.30 3.30 3.30 3.30 3.30 3.30
   AL -16.36992 -4.70 -3.62 -2.81 2.01 2.33 2.45 2.49 2.50 2.50 2.50 2.50 2.50 2.50 2.50
   AR -17.14379 -5.72 -4.28 -3.17 1.84 2.27 2.43 2.49 2.50 2.50 2.50 2.50 2.50 2.50 2.50

   ;


Accepted Solutions
Solution
‎06-10-2014 09:16 AM
Super User
Posts: 10,020

Re: Datalines/Cards Keywords

Posted in reply to maroulator

This worked for me:

data med;

   input location : $2. hpa1-hpa15;

   cards;

   US -17.63753 -4.98 -3.21 -2.10 2.83 3.14 3.26 3.29 3.30 3.30 3.30 3.30 3.30 3.30 3.30

   AK -14.09531 -2.62 -1.92 -1.47 3.10 3.23 3.28 3.30 3.30 3.30 3.30 3.30 3.30 3.30 3.30

   AL -16.36992 -4.70 -3.62 -2.81 2.01 2.33 2.45 2.49 2.50 2.50 2.50 2.50 2.50 2.50 2.50

   AR -17.14379 -5.72 -4.28 -3.17 1.84 2.27 2.43 2.49 2.50 2.50 2.50 2.50 2.50 2.50 2.50

   ;

   run;

Xia Keshan

View solution in original post


All Replies
Super User
Posts: 19,770

Re: Datalines/Cards Keywords

Posted in reply to maroulator

This worked for me:

data med;

    informat location $2.;

   input location $ hpa1-hpa15;

   cards;

etc...

Super User
Super User
Posts: 7,039

Re: Datalines/Cards Keywords

I prefer to use LENGTH (or ATTRIB) statement to set the length of variable rather than depending on a side effect of the INFORMAT statement.

Also once you have defined the variable as character you do not need to add the $ to the INPUT statement.

Solution
‎06-10-2014 09:16 AM
Super User
Posts: 10,020

Re: Datalines/Cards Keywords

Posted in reply to maroulator

This worked for me:

data med;

   input location : $2. hpa1-hpa15;

   cards;

   US -17.63753 -4.98 -3.21 -2.10 2.83 3.14 3.26 3.29 3.30 3.30 3.30 3.30 3.30 3.30 3.30

   AK -14.09531 -2.62 -1.92 -1.47 3.10 3.23 3.28 3.30 3.30 3.30 3.30 3.30 3.30 3.30 3.30

   AL -16.36992 -4.70 -3.62 -2.81 2.01 2.33 2.45 2.49 2.50 2.50 2.50 2.50 2.50 2.50 2.50

   AR -17.14379 -5.72 -4.28 -3.17 1.84 2.27 2.43 2.49 2.50 2.50 2.50 2.50 2.50 2.50 2.50

   ;

   run;

Xia Keshan

Super User
Posts: 5,497

Re: Datalines/Cards Keywords

Posted in reply to maroulator

Your problem is caused by reading in LOCATION incorrectly.  The instruction $2 tells says to take the contents of column 2 as the value of LOCATION.  Because you have indented your data, that gives you "U", "A", "A", and "A" for these 4 lines of data.

The invalid data comes about as the INPUT statement continues, reading the value of HPA1 beginning in column 3.  Obviously, values like "S", "K", "L", and "R" are not the right values for HPA1 and are (as SAS noted) invalid numeric values.

Fix the reading of LOCATION and the rest will be fine.

🔒 This topic is solved and locked.

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

Discussion stats
  • 4 replies
  • 268 views
  • 5 likes
  • 5 in conversation