用 space SAS 替换一列中的任何回车 returns / 新行
Replace any carriage returns / new lines in one column with a space SAS
我在 SAS 中有很多数据,其中一列有自由文本描述,其中一些有回车符 returns / 我想删除的新行替换为空格。
我有一些代码可以删除回车符 returns / 新行,但这并不是很有帮助,因为这并没有用任何东西替换它,所以它只是合并行和单词最终被混在一起。
作为一个例子,我得到了以下数据(好吧,除了最后两行实际上应该是一行,不知道如何在此处正确输入它(任何帮助将不胜感激!)) :
ID
Description
Col3
Col4
Col5
Col6
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
我想要:
ID
Description
Col3
Col4
Col5
Col6
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
但我得到:
ID
Description
Col3
Col4
Col5
Col6
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 mugmug joule
C3
0
20
1
下面的代码...但我无法弄清楚如何实际输入一些示例数据,其中包含回车 return / 换行符。有谁知道这是怎么做到的吗?但是条目号 6 应该有一个回车符 return / 换行符。
data data_1;
Infile Datalines dlm=',';
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;
/* removes new line but doesn't replace it with a space */
data data_2;
set data_1;
Description = COMPRESS(Description, , "kw");
run;
如有任何帮助,我们将不胜感激。
不要使用 COMPRESS() 删除字符,而是使用 TRANSLATE() 替换它们。
如果您只想删除 CR 和 LF 字符,请使用:
description = translate(description,' ','0D0A'x);
或者,如果您想用空格替换字符,'w' 压缩选项将 select 然后使用此:
description=translate(description,' ',compress(description, ,'w'));
PS 如果您想在测试数据中引入一些 CR 字符,您还可以使用 TRANSLATE() 来替换一些其他字符,例如竖线。
data have;
infile datalines dsd truncover;
input ID Description :. Col3 $ Col4 Col5 Col6;
description=translate(description,'0D'x,'|');
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
;
我在 SAS 中有很多数据,其中一列有自由文本描述,其中一些有回车符 returns / 我想删除的新行替换为空格。
我有一些代码可以删除回车符 returns / 新行,但这并不是很有帮助,因为这并没有用任何东西替换它,所以它只是合并行和单词最终被混在一起。
作为一个例子,我得到了以下数据(好吧,除了最后两行实际上应该是一行,不知道如何在此处正确输入它(任何帮助将不胜感激!)) :
ID | Description | Col3 | Col4 | Col5 | Col6 |
---|---|---|---|---|---|
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 |
我想要:
ID | Description | Col3 | Col4 | Col5 | Col6 |
---|---|---|---|---|---|
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 |
但我得到:
ID | Description | Col3 | Col4 | Col5 | Col6 |
---|---|---|---|---|---|
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 mugmug joule | C3 | 0 | 20 | 1 |
下面的代码...但我无法弄清楚如何实际输入一些示例数据,其中包含回车 return / 换行符。有谁知道这是怎么做到的吗?但是条目号 6 应该有一个回车符 return / 换行符。
data data_1;
Infile Datalines dlm=',';
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;
/* removes new line but doesn't replace it with a space */
data data_2;
set data_1;
Description = COMPRESS(Description, , "kw");
run;
如有任何帮助,我们将不胜感激。
不要使用 COMPRESS() 删除字符,而是使用 TRANSLATE() 替换它们。
如果您只想删除 CR 和 LF 字符,请使用:
description = translate(description,' ','0D0A'x);
或者,如果您想用空格替换字符,'w' 压缩选项将 select 然后使用此:
description=translate(description,' ',compress(description, ,'w'));
PS 如果您想在测试数据中引入一些 CR 字符,您还可以使用 TRANSLATE() 来替换一些其他字符,例如竖线。
data have;
infile datalines dsd truncover;
input ID Description :. Col3 $ Col4 Col5 Col6;
description=translate(description,'0D'x,'|');
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
;