hyg17
hyg17

Reputation: 237

How to read instream data

I know that this is a very basic question, but I really am having trouble.

I wrote the following code and all I want to do is to read the data correctly, but I cannot find a way to instruct SAS to read the BMI.

There are two things I want to do.

1), Have SAS store the entire number including all the decimals. 2), when printed, I would like to approximate the number to two decimal points.

data HW02EX01;
  input Patient 1-2  Weight 5-7 Height 9-10 Age 13-14 BMI 17-26 Smoking $ 29-40 Asthma $ 45-48;
  cards;
14  167 70  65  23.9593878  never           no
run;

Note: I left only the first observation since the display becomes really ugly and wearisome to edit by hand.

Upvotes: 0

Views: 147

Answers (2)

Reeza
Reeza

Reputation: 21274

See the comments in the code, specifically the usage of LENGTH, FORMAT and INFORMAT statements to control the input and output appearance of data.

data HW02EX01;
    *specify the length of the variables;
    length patient $8. weight height age bmi 8. smoking asthma $8.; 


    *specify the informats of the variables; 
    *an informat is does the variable look like when trying to read it in;
    informat patient $8. weight height age bmi best32.;

    *formats control how infomraiton is displayed in output/tables;
    format bmi 32.2 weight height age 12.;

    input Patient $ Weight Height Age BMI Smoking  Asthma ;
    cards;
14 167 70 65 23.9593878 never no
;

run;

Upvotes: 1

yebusa
yebusa

Reputation: 31

friend.

Maybe it could be useful the following code:

data HW02EX01_;
    input Patient Weight Height Age BMI Smoking : $20. Asthma $10.;
    format BMI 32.2;
    cards;
14 167 70 65 23.9593878 never no
;

By way of comment, I would like to indicate some details:

  • If your input data has a fixed length, use the column reading method as you propose in your code in the input statement. If not, use a list reading entry (assuming there is a delimiter between your input data).
  • When SAS reads in the list form, it converts and saves all the digits of a numerical value. So you do not have to worry about reading decimals too.
  • To display a numerical value the way you like it, you can use the format statement to assign a representation of the value. In this case with two decimals we use the format "w.d". Where w is the total length of the number that can occur and d indicates the number of decimals to show. It should be mentioned that the formats do not change the value of the variable, only its presentation.
  • When using the cards statement, it is not necessary to use a run statement.

I hope it will be useful.

Upvotes: 2

Related Questions