【问题标题】:Create new variable combining three other variables without using ifelse in sas在 sas 中不使用 ifelse 的情况下创建结合其他三个变量的新变量
【发布时间】:2018-01-24 03:06:11
【问题描述】:

我需要创建一个变量,它结合了三个变量 age 、 sexe 和 agios。它们都位于 sas 表中,并且都是数字的。

第一步,我将它们转换为分类变量,然后我建议使用 compress 函数来创建这个新变量:

data new;
set new;

attrib sexe format=$15.;
if sexe=1 then sexe="HOMME";
else if sexe=0 then sexe="FEMME";

attrib agios format=$15.;
if agios=0 then agios="NON_AGIOS";
else AGIOS="AGIOS";

attrib age format=$15.;
if (age<=0) and (age=>25) then age="a25";
if (age<=26) and (age=>40) then age="a40";
if (age<=41) and (age=>65) then age="a60";
if (age=>65) then age="a65";
new_variable=compress(agios||sexe||age);
run; 

  But I have an warnings repeted to all the concerned variables:

警告:变量 agios 已被定义为数字。

并且表中的变量没有任何转换。此外,相关变量 age 、 sexe 和 agios 的值从表中消失。它开始是空的。

请问我该如何纠正这个问题? 或者有没有其他建议不使用 ifelse 来创建新变量?

谢谢

【问题讨论】:

标签: sas


【解决方案1】:

这个问题在这一行:

if agios=0 then agios="NON_AGIOS";

编译器将 agios 初始化为数值变量。然后,您不能为其分配字符值。解决这个问题的一种方法是创建一个新变量(agios2)然后重命名它,例如:

data new;
set new(rename=(agios=agios2 sexe=sexe2 age=age2)); /* rename vars on input */

attrib sexe format=$15.;
if sexe2=1 then sexe="HOMME";
else if sexe2=0 then sexe="FEMME";

attrib agios format=$15.;
if agios2=0 then agios="NON_AGIOS"; /* reference the renamed variable */
else AGIOS="AGIOS";
drop agios2 age2 sexe2;             /* drop the renamed variables on output */

attrib age format=$15.;
if (age2<=0) and (age2=>25) then age="a25";
if (age2<=26) and (age2=>40) then age="a40";
if (age2<=41) and (age2=>65) then age="a60";
if (age2=>65) then age="a65";
new_variable=compress(agios||sexe||age);
run; 

【讨论】:

  • 谢谢你的回答,但它只适用于agios,我如何为age和sexe添加rename函数??
  • 它只是不适用于age 变量,该列是空的并且没有转换?我决定将drop agios2 age2 sexe2; 放在new_variable=compress(agios||sexe||age); 之后和之前,它也不起作用!
  • 我发现年龄有错字,我更新了,现在可以用了吗?
  • drop 语句的位置不相关(只在编译时考虑)
【解决方案2】:

另一种方法是使用(NOTSORTED) 选项为每个变量创建一个序列格式。报告时使用这些格式。例如在Proc TABULATE 中的FORMAT 语句和带有选项order=formatted preloadfmt 的CLASS 语句。

您发布的代码中的年龄分类显示不正确

if (age<=0) and (age=>25) then age="a25";

年龄值不能同时= 25。

在标记范围时,我建议使用与其所代表的范围一致的值。例如,有时您在标签 (a25, a40) 中使用端点值,而在其他时候使用中间点 (a60)。方案不一致。

这是在Proc TABULATE 中输出时将格式应用于原始数据的示例代码。

proc format;
  value sexe (notsorted)
    0 = 'HOMME'
    1 = 'FEMME'
  ;

  value agios (notsorted)
    0 = 'NON_AGIOS'
    1 = 'AGIOS'
  ;

  value age (notsorted)
     0< - 25 = 'a25'
    25< - 40 = 'a40'
    40< - 65 = 'a60' /* inconsistent labeling of range */
    65< - high = 'a65'
  ;

  * mock data;
  data have;
    do personid = 1 to 100;
      sexe = ranuni(123) < 0.40;
      agios = ranuni(123) > 0.65;
      age = floor(12 + 58 * ranuni(123));
      output;
    end;
  run;

  proc tabulate data=have;
    class sexe agios age / order=formatted preloadfmt;
    classlevel sexe agios age / style=[pretext='A0A0A0A0'x];
    format sexe sexe. agios agios. age age.;
    table
      ( sexe agios age ) 
    , N
    / nocellmerge
    ;
  run;

有时创建新数据并将格式化值变成实际数据是合适的,尤其是在将数据交给可能没有 SAS 和格式化代码的其他用户时。

data ...;
  set ...;
  ...
  sexe_fv = put (sexe,sexe.);  * fv stands for formatted value;
  agios_fv = put (agios, agios.);
  age_fv = put (age, age.);
  ...
run;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-27
    相关资源
    最近更新 更多