You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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 use input(Height,w.d) as a standalone statement, which isn't valid SAS syntax. The input() function needs to assign the converted numeric value to a new variable (since you read Height as a character with the $ modifier).
  • Missing semicolon: The line bmi_diff = bmi_init - bmi_final didn'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 just weight(kg)/height(m).
  • Character variable used in math: Since Height was 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.dat entries (like 00768155150) fit the fixed column layout you defined perfectly:
    • Subj: positions 1-3 → 007
    • Height: positions 4-5 → 68
    • Wt_init: positions 6-8 → 155
    • Wt_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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 11:17:55