I have a question about SAS what if statement. Say that we have the following program:
data work.stress;
infile tests;
input ID Name $ Tolerance;
if tolerance <= 5 then tolerance = "Low";
else if tolerance >= 6 then tolerance = "High";
run;
I know we should always make sure data intervals has no gap between them, but just say this IS the program we are running, what if there's a observation that has a tolerance as 5.5? Will the output show a Syntax error?
Thanks!
JessX
答案 0 :(得分:0)
When using if-then-else
there is no syntax error caused by failing to fit in any particular if/else statement; it just doesn't do anything. So in your case, tolerance
would have its original value if it had value 5.5. This would be commonly done in cases where you want to censor extreme values, though you'd use a special missing usually (assuming you want these extreme values excluded from the analysis):
data work.stress;
infile tests;
input ID Name $ Tolerance;
if tolerance <= 5 then tolerance = .L;
else if tolerance >= 6 then tolerance = .H;
run;
(In fact, it will have missing values for the "High" "Low" groups as you wrote it, since you defined it as a numeric value.)
If you use select-when
, then you do have to account for every single possible value - if you don't have an otherwise
, it will fail on any value not covered by a when
. This is part of why it's helpful to use select-when
.
答案 1 :(得分:0)
会有错误,但不是你提到的原因。如果您尝试用字符串替换数字,SAS将不允许它。
两种可能的途径:
proc format
声明格式,您可以在其中指定阈值。落在阈值裂缝之间的值将按原样显示(数字)。在这个例子中,除了你添加一个&#34; MID&#34;之外,5到6之间的任何值都会有新的tolerance
变量的缺失值。类别:
data work.stress;
infile tests(rename=(Tolerance=Tol_num));
input ID Name $ Tolerance;
format tolerance $8.;
if tol_num = . then tolerance = "?";
else if tol_num <= 5 then tolerance = "LOW";
else if tol_num >= 6 then tolerance = "HIGH";
else tolerance = "MID";
drop tol_num;
run;