如果有其他情况,请在SAS中执行

如果有其他情况,请在SAS中执行,sas,Sas,嗨,我想知道多重if-then条件的步骤 我想做以下工作: data _ ; set _ ; if condition 1 is true then do; if sub condition 1 is true then _ ; else if sub condition2 is true then _; else if ... ; end; else if condition 2 is true then do; /* Is it right? */ if sub condition 1 i

嗨,我想知道多重if-then条件的步骤

我想做以下工作:

data _ ;
set _ ;
if condition 1 is true then do; 
if sub condition 1 is true then _ ;
else if sub condition2 is true then _;
else if ... ;
end;
else if condition 2 is true then do; /* Is it right? */
if sub condition 1 is true then _ ;
else if sub condition2 is true then _;
else if ... ;
end;
run;
你能告诉我正确的步骤是什么吗?我应该包括其他,如果或其他做

例如:条件1可以取值1或0。子条件(我将它们称为test1、test2、test3等)是其他条件。所以我想说:

data _ ;
set _ ;
if condition1 = 1 then do;
if test1 = . then test3=test2; else test3=test1; 
else if test1 = 'My test' or test2= 'My test' then test3=test2 else test3=test2; 
end; 
else if condition1=0 then do;
if test1 = . then test3=test2; else test3=test1; 
else if test1 = 'My test' or test2= 'My test' then test3=test2 else test3=test2; 
end; 
else test3=test2;
run; 
数据样本可以是:

condition1   test1        test2
1             .            M
0             My test      .
1             Love        home
0             Home         .
我想选择的是,基于条件1的值

如果条件1为1,test1为。然后分配给test3 test2的值,否则test3=test1;等等

我的预期结果是:

condition1   test1        test2       test3
1             .            M           M
0             My test      .           My test
1             Love        home         Love
0             Home         .           Home

不知道你在问什么,但也许这会对你有所帮助

您可以将嵌套的ifs视为附加条件。所以如果你有

if test1 then do;
  if test2 then statement1 ;
  else if test3 then statement2 ;
end;
你可以把它改写成

if test1 and test2 then statement1 ;
else if test1 and test3 then statement2 ;

我不太清楚问题是什么。DO/END块只是为了让您可以用多个语句替换单个语句。在您的示例中,变量TEST1是数字还是字符?在某些地方,您将其视为数字,
test1=。
,在其他地方,您将其视为字符,
test1='My test'
。您可以使用
missing()
函数来测试变量是否丢失,它对数字或字符变量的作用相同,此外,它还将检测我找到的数据集中的特殊缺失值,如
.a
.Z
等。和NA值(后者作为字符串)。有没有办法检查变量的类型?PROC内容将向您显示有关数据集的信息。谢谢Tom。那么,同样的方法也适用于你,那么你呢?有两种不同的条件(例如条件1=1,条件2是条件1的另一个值,如果是二进制的,即0)。然后我考虑子条件(您的Test2和Test3)。问题在于逻辑。我没有一个数据集,但是如果你愿意的话,我想有一个可能适合的例子,我在我的问题中包括了一个可能的例子。感谢您的帮助,看看这两种方法是否相同,考虑每一个可能的测试条件值。因此,如果你有两个测试,你正在做有4种可能的组合,FF,FT,TF和TT。考虑每一个并检查两个程序是否产生相同的结果。对于N个测试,有2**N个可能的组合。谢谢Tom。我会检查一下,然后告诉你