将变量中的单词添加双引号。

huangapple go评论85阅读模式
英文:

SAS : add double quotes to words in a variable

问题

需要将SAS数据集变量中的每个单词用双引号括起来。

例如:

have1=ABC => need1="ABC"
have1=ABC,XYZ => need1="ABC", "XYZ"
have1=ABC,PQR,KLM => need1="ABC", "PQR", "KLM"

我搜索了一下,尝试了类似这样的方法,但不起作用:

need1=cat('"', tranwrd(have1, " ", '", "'), '"');
英文:

SAS dataset variable has one or more words. I want to add double quotes to each word.

e.g.

have1=ABC => need1="ABC"
have1=ABC,XYZ => need1="ABC", "XYZ"
have1=ABC,PQR,KLM => need1="ABC", "PQR", "KLM"

I searched around and tried something like this but didn't work

need1=cat('"',tranwrd(have1," ",'" "'),'"');

答案1

得分: 1

尝试这个

data have;
    s = "ABC        "; output;
    s = "ABC,XYZ    "; output;
    s = "ABC,PQR,KLM"; output;
run;

data want;
   set have;
   ss = prxchange('s/([A-Z]+)/"$1"/', -1, s);
run;
英文:

Try this

data have;
s = "ABC        "; output;
s = "ABC,XYZ    "; output;
s = "ABC,PQR,KLM"; output;
run;

data want;
   set have;
   ss = prxchange('s/([A-Z]+)/"$1"/', -1, s);
run;

答案2

得分: 0

你的原始代码是:

need1=cat('\"',tranwrd(have1," ","\" \""),'\"');

这是一个正确的思路。你只需在其中添加一个逗号,就可以自己解决这个问题。

尝试这个:

need1=cats('\"',tranwrd(have1,' ','\",\"'),'"');

英文:

Your original code is

need1=cat('"',tranwrd(have1," ",'" "'),'"');

That is a correct thinking.All you have to do is simply adding a “,” in it ,and you will solve this problem by yourself.

Try this:

need1=cats('"',tranwrd(have1,',','", "'),'"');

答案3

得分: 0

你可以使用正则表达式来将所有找到的单词的出现替换为带引号的单词。如果你不熟悉正则表达式模式,捕获模式在括号内,\b 表示单词边界,.+ 表示边界之间至少一个字符,? 表示不贪婪,所以不要在匹配时吞噬其他边界。"$1" 替换了匹配的内容,用双引号包裹起来。

data have;
length list $50;
input list;
datalines;
ABC
ABC,XYZ
ABC,PQR,KLM
123,456,789
;

data want;
set have;
qlist = prxchange('s/(\b.+?\b)/"$1"/',-1,list);
put qlist;
run;


<details>
<summary>英文:</summary>

You can use a regular expression to replace all occurrences of found words with the words quoted.  If you are not familiar with regex patterns, the capture pattern is inside the parentheses, and the \b indicates a word boundary, .+ means at least one character between the boundaries, and ? means don&#39;t be greedy, so don&#39;t gobble up other boundaries when matching.  &quot;$1&quot; replaces what was matched with what was matched inside double quotes.

data have;
length list $50;
input list;
datalines;
ABC
ABC,XYZ
ABC,PQR,KLM
123,456,789
;

data want;
set have;
qlist = prxchange('s/(\b.+?\b)/"$1"/',-1,list);
put qlist;
run;


</details>



# 答案4
**得分**: 0

你可以使用 `tranwrd` 函数,并在双引号内包装它与 `cats`。

```sas
data test;
have1=ABC,PQR,KLM;
want1=cat(&#39;&quot;&#39;,tranwrd(have1,&#39;,&#39;,&#39;&quot;,&quot;&#39;),&#39;&quot;&#39;);
run;
英文:

You can use tranwrd and wrap it in quotes wit cats

data test;
have1=ABC,PQR,KLM;
want1=cat(&#39;&quot;&#39;,tranwrd(have1,&#39;,&#39;,&#39;&quot;,&quot;&#39;),&#39;&quot;&#39;);
run;

huangapple
  • 本文由 发表于 2023年3月7日 11:48:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75657892.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定