You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SAS中如何将单列内的回车/换行符替换为空格避免单词粘连

SAS文本换行符处理及DATALINES样本构造方案

1. 换行/回车符替换为空格实现逻辑

原有代码中COMPRESS函数的kw参数会直接移除所有空白类控制字符(包含回车、换行、制表符等),无占位保留,因此会出现单词粘连问题。正确逻辑为:先将回车、换行符显式替换为空格,再按需清理多余空格。
修正代码如下:

data data_2;
set data_1;
/* 按顺序将换行符(0A)、回车符(0D)替换为空格 */
Description = TRANSLATE(Description, '  ', '0A0D'x);
/* 可选操作:清理首尾空格、将连续多个空格合并为单个空格 */
Description = COMPBL(STRIP(Description));
run;

上述代码处理ID6的记录后,即可得到预期结果sky computer mug mug joule。

2. DATALINES中输入带换行符样本的方法

方法1:赋值拼接法(最可控,无格式歧义)

通过select分支赋值,直接用十六进制换行符常量'0A'x拼接构造带换行的文本,避免DATALINES换行识别问题:

data data_1;
format ID 8. Description $char50. Col3 $2. Col4 Col5 Col6 8.;
input ID Col3 $ Col4 Col5 Col6;
select(ID);
    when(1) Description = 'bla bla bla';
    when(2) Description = 'got up tear';
    when(3) Description = 'free text';
    when(4) Description = 'house roof tree';
    when(5) Description = 'house roof tree';
    when(6) Description = 'sky computer mug'||'0A'x||'mug joule';
    otherwise;
end;
datalines;
1,C1,0,100,0
2,C1,0,0,0
3,C1,10,100,0
4,C1,10,100,0
5,C1,10,0,0
6,C3,0,20,1
;
run;

方法2:字段包裹+INFILE参数配置

若要直接在DATALINES中写入实体换行,需要给带换行的字段包裹双引号,同时在INFILE语句中添加DSD参数,让SAS识别字段内的引号包裹内容,不会把字段内的换行判定为记录分隔符:

data data_1;
Infile Datalines dlm=',' dsd missover;
format Description $char50.;
input ID Description $ Col3 $ Col4 Col5 Col6;
datalines;
1,bla bla bla,C1,0,100,0
2,got up tear,C1,0,0,0
3,free text,C1,10,100,0
4,house roof tree,C1,10,100,0
5,house roof tree,C1,10,0,0
6,"sky computer mug 
mug joule",C3,0,20,1
;
run;

内容的提问来源于stack exchange,提问作者uwd

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 10:06:02