如何在SAS代码中添加指定变量?遇BMI_INIT等变量未找到错误
Fixing SAS Error 263: Variables BMI_INIT/BMI_FINAL/BMI_DIFF Not Found
Let's break down what's causing your errors and fix the code properly:
Key Issues in Your Original Code
- Invalid
input()function usage: You tried to useinput(Height,w.d)as a standalone statement, which isn't valid SAS syntax. Theinput()function needs to assign the converted numeric value to a new variable (since you readHeightas a character with the$modifier). - Missing semicolon: The line
bmi_diff = bmi_init - bmi_finaldidn't end with a semicolon, which broke SAS's ability to parse and create your variables. - Incorrect BMI formula: You forgot to square the height value in meters—BMI is calculated as
weight(kg) / height(m)², not justweight(kg)/height(m). - Character variable used in math: Since
Heightwas read as a character type, using it directly in calculations would cause errors even if other issues were fixed.
Corrected SAS Code
DATA dietdata; infile 'Downloads/dietdata.dat'; * Read raw data using fixed column positions; input Subj $ 1-3 Height $ 4-5 Wt_init 6-8 Wt_final 9-11; * Convert character Height to numeric for calculations; Height_num = input(Height, w.d); * Calculate initial BMI (correct formula: kg/m²); bmi_init = (Wt_init / 2.2) / ( (Height_num / 0.0254) ** 2 ); * Calculate final BMI; bmi_final = (Wt_final / 2.2) / ( (Height_num / 0.0254) ** 2 ); * Calculate difference between initial and final BMI; bmi_diff = bmi_init - bmi_final; RUN; PROC PRINT data = dietdata; var Subj bmi_init bmi_final bmi_diff; RUN;
Additional Notes
- Verify your input data format: Your
dietdata.datentries (like00768155150) fit the fixed column layout you defined perfectly:Subj: positions 1-3 →007Height: positions 4-5 →68Wt_init: positions 6-8 →155Wt_final: positions 9-11 →150
SAS will read these correctly as fixed-length entries, even if they're all on one line in the .dat file.
- Unit conversions: The factors
/2.2(pounds to kg) and/0.0254(inches to meters) are correct for your input data units.
内容的提问来源于stack exchange,提问作者Dillon Hector
相关产品推荐
相关产品推荐

