重叠范围SAS

时间:2016-02-09 18:14:14

标签: variables sas

我正在尝试在SAS中创建一个新变量并使用以下代码:

    if (Age >= 16) and (Age <= 85) then Age_New = "Total, 16 years and over";
else if (Age >= 16) and (Age <= 19) then Age_New = "16 to 19 years";
else if (Age >= 20) and (Age <= 85) then Age_New = "20 years and over";
else if (Age >= 20) and (Age <= 24) then Age_New = "20 to 24 years";
else if (Age >= 25) and (Age <= 85) then Age_New = "25 years and over";
else if (Age >= 25) and (Age <= 54) then Age_New = "25 to 54 years";
else if (Age >= 55) and (Age <= 85) then Age_New = "55 years and over";
else if (Age >= 55) and (Age <= 64) then Age_New = "55 to 64 years";
else if (Age >= 65) and (Age <= 85) then Age_New = "65 years and over";
else Age_New = "Other";

不管发生什么事情,我得到了所有观察结果的“总计,16年及以上”的值,我理解为什么。如何编辑代码来完成我想要实现的任务?

2 个答案:

答案 0 :(得分:2)

如果您不想使用格式,则可以创建观察结果。

data have;
   input age;
   cards;
16
18
22
33
44
55
66
77
;;;;
   run;
data need;
   set have;
   if (Age >= 16) and (Age <= 85) then do; Age_New = "Total, 16 years and over"; output; end;
   if (Age >= 16) and (Age <= 19) then do; Age_New = "16 to 19 years";           output; end;
   if (Age >= 20) and (Age <= 85) then do; Age_New = "20 years and over";        output; end;
   if (Age >= 20) and (Age <= 24) then do; Age_New = "20 to 24 years";           output; end;
   if (Age >= 25) and (Age <= 85) then do; Age_New = "25 years and over";        output; end;
   if (Age >= 25) and (Age <= 54) then do; Age_New = "25 to 54 years";           output; end;
   if (Age >= 55) and (Age <= 85) then do; Age_New = "55 years and over";        output; end;
   if (Age >= 55) and (Age <= 64) then do; Age_New = "55 to 64 years";           output; end; 
   if (Age >= 65) and (Age <= 85) then do; Age_New = "65 years and over";        output; end;
   run;
proc print;
   run;
proc freq;
   tables age_new / list nocum nopercent;
   run;

答案 1 :(得分:0)

你的if-then-else条款不是互斥的。所有年龄<= 85的if语句都包含所有数字,因此第一个&if-statement语句&#39;具有Age&lt; = 85的将被强制执行。我拿出你所放置的所有陈述和(年龄<= 85)并显示了预期的结果。

data have;
   input age;
   cards;
16
18
22
33
44
55
66
77


;
data have;
   set have;
if (Age >= 16) and (Age <= 19) then Age_New = "16 to 19 years";
else if (Age >= 20) and (Age <= 24) then Age_New = "20 to 24 years";
else if (Age >= 25) and (Age <= 54) then Age_New = "25 to 54 years";
else if (Age >= 55) and (Age <= 64) then Age_New = "55 to 64 years";
else Age_New = "Other";
run;

enter image description here