搜索和替换无法按预期在 sas 中工作
search and replace not working in sas as expected
我正在尝试将 list1 中的单词替换为 list2(基本上是从一种语言翻译成另一种语言),但搜索和替换功能没有按预期工作。它只替换 ww 字符串的一部分而不是完整的。我做错了什么吗?
在运行上面的代码之后我是运行的输出:
names_list names_list_french i
My name is Craig Matthews, My name is Donald Dunn Je m'appis Craig Matthews, Je m'appis Donald Dunn 3
预期输出:
names_list names_list_french i
My name is Craig Matthews, My name is Donald Dunn Je m'appelle Craig Matthews, Je m'appelle Donald Dunn 3
SAS CODE:
data datatable;
names_list="My name is Craig Matthews, My name is Donald Dunn";
array list1{2} $ _temporary_ (
"My name is Craig Matthews",
"My name is Donald Dunn"
);
array list2{2} $ _temporary_ (
"Je m'appelle Craig Matthews",
"Je m'appelle Donald Dunn"
);
names_list_french=names_list;
put names_list= names_list_french=;
do i=1 to dim(list1);
put list1{i}= list2{i}=;
names_list_french=tranwrd(names_list_french,list1{i},list2{i});
end;
put names_list= names_list_french=;
run;
SAS 将字符串存储为用空格填充的固定长度。
如果你不告诉它,否则 SAS 将默认字符变量的长度为 8。
因此定义变量的长度并在将字符串传递给 TRANWRD() 函数时使用 TRIM()。
data datatable;
length names_list 0;
names_list="My name is Craig Matthews, My name is Donald Dunn";
array list1{2} 0 _temporary_ (
"My name is Craig Matthews",
"My name is Donald Dunn"
);
array list2{2} 0 _temporary_ (
"Je m'appelle Craig Matthews",
"Je m'appelle Donald Dunn"
);
names_list_french=names_list;
put names_list= names_list_french=;
do i=1 to dim(list1);
put list1{i}= list2{i}=;
names_list_french=tranwrd(names_list_french,trim(list1{i}),trim(list2{i}));
end;
put names_list= names_list_french=;
run;
我正在尝试将 list1 中的单词替换为 list2(基本上是从一种语言翻译成另一种语言),但搜索和替换功能没有按预期工作。它只替换 ww 字符串的一部分而不是完整的。我做错了什么吗?
在运行上面的代码之后我是运行的输出:
names_list names_list_french i
My name is Craig Matthews, My name is Donald Dunn Je m'appis Craig Matthews, Je m'appis Donald Dunn 3
预期输出:
names_list names_list_french i
My name is Craig Matthews, My name is Donald Dunn Je m'appelle Craig Matthews, Je m'appelle Donald Dunn 3
SAS CODE:
data datatable;
names_list="My name is Craig Matthews, My name is Donald Dunn";
array list1{2} $ _temporary_ (
"My name is Craig Matthews",
"My name is Donald Dunn"
);
array list2{2} $ _temporary_ (
"Je m'appelle Craig Matthews",
"Je m'appelle Donald Dunn"
);
names_list_french=names_list;
put names_list= names_list_french=;
do i=1 to dim(list1);
put list1{i}= list2{i}=;
names_list_french=tranwrd(names_list_french,list1{i},list2{i});
end;
put names_list= names_list_french=;
run;
SAS 将字符串存储为用空格填充的固定长度。 如果你不告诉它,否则 SAS 将默认字符变量的长度为 8。
因此定义变量的长度并在将字符串传递给 TRANWRD() 函数时使用 TRIM()。
data datatable;
length names_list 0;
names_list="My name is Craig Matthews, My name is Donald Dunn";
array list1{2} 0 _temporary_ (
"My name is Craig Matthews",
"My name is Donald Dunn"
);
array list2{2} 0 _temporary_ (
"Je m'appelle Craig Matthews",
"Je m'appelle Donald Dunn"
);
names_list_french=names_list;
put names_list= names_list_french=;
do i=1 to dim(list1);
put list1{i}= list2{i}=;
names_list_french=tranwrd(names_list_french,trim(list1{i}),trim(list2{i}));
end;
put names_list= names_list_french=;
run;