sas 数据步骤中 if else 语句的通配符

Wildcard for an if else statement in sas data step

我想根据特定字符串对调查数据中的新变量进行编码,但使用 sas 数据步骤在该字符串前后包含一个通配符以获取附加信息。下面的代码匹配字符串开头的模式

data survey_data;
    set final_data;
        if 'var1' =: 'this string' then var2 = 'Yes';
        else var2 = 'No';   
run;

但我不确定如果它出现在字符串中间,如何让它匹配模式,':=:' 不起作用。

使用index函数:

data survey_data ;
  set final_data ;
  if index(var1,'this string') then var2 = 'Yes' ;
  else                              var2 = 'No' ;
run ;

INDEX 函数 returns 一个数值,表示 var1 中找到 'this string' 的第一个字符位置。如果没有找到INDEX returns零。 SAS 中没有比较运算符 (=>><,eq,gt) 的 if 语句如果具有正的非零值则解析为真,因此:

if index(var1,'this string') then

是 shorthand 用于:

if index(var1,'this string') > 0 then

SAS INDEX 函数 > https://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000212242.htm