【问题标题】:SAS remove special characters from stringSAS从字符串中删除特殊字符
【发布时间】:2019-10-07 16:27:09
【问题描述】:

下午好, 我有一个包含名字和姓氏列表的数据集。无论出于何种原因,其中一些名称中都包含需要删除的特殊字符。该字段为字符格式,我尝试使用以下内容仅保留允许的字符并删除所有其他字符。但是,我的结果返回所有名称的空白值。

我在这里做错了什么?

Data want; 
set have;
'Last Name'n=compress(last_name,"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'- ","k");
'First Name'n=compress(first_name,"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'- ","k");
drop last_name first_name;
run;

【问题讨论】:

  • 显示您的日志。您确定您的输入数据集包含名为 last_namefirst_name 的变量吗?

标签: sas compression


【解决方案1】:

用示例数据集编辑

 Data have;
    INPUT last_name $ 1-14  first_name $;
    datalines;
    thomas*   James 
    Kory-Elk  Nick
    ^Shaffner Fun
    ;
 Run;

 Data want; 
   set have;
     Last_Name_New=compress(last_name,"abcdefghijklmnopqrstuvwxyz-","kis");
     First_Name_New=compress(first_name,"abcdefghijklmnopqrstuvwxyz-","kis");
    drop last_name first_name;
 run;
 /* 'k' keeps the characters in the list instead of removing them.
    'i'  ignores the case of the characters to be kept or removed.
    's' adds space characters (blank, horizontal tab, vertical tab, carriage return, line feed, and form feed) to the list of characters.*/

【讨论】:

  • 看看我上面编辑的解决方案。问题似乎出在为每个名字和姓氏命名新变量。
猜你喜欢
  • 2011-04-11
  • 2016-01-23
  • 2019-11-09
  • 2011-08-29
  • 1970-01-01
相关资源
最近更新 更多