我有一个包含单词同义词信息(多行)的数据集,下面是数据集的简要示例。给出了每个单词的同义词信息。
Word Synonym
C01 C02
C01 C05
C02 C02
C02 C05
C03 C04
C05 C06
C11 C12
.. ..从上面的数据集中,单词-同义词的关系可以识别如下。
C01-C02-C05-C06
C03-C04
C11-C12
执行完sas代码后,我想要一个如下所示形式的数据集。
Word Synonym1 Synonym2 Synonym3
C01 C02 C05 C06
C03 C04
C11 C12我尝试了内部连接的冗余步骤,但似乎有很多不必要的过程。
发布于 2018-08-07 23:56:07
我很难在SAS中找到一个好的解决方案(在其他语言中,这更容易解决)。下面的方法不是很好,因为它试图将所有组写入单个变量中,如果您有很多记录,这个变量很快就会用完。另外,它依赖于'#‘作为分隔符。如果您的单词可以包含此字符,您可能希望将其更改为不同的字符。
data groups;
set testData nObs=numObs;
array groups [*] $32767 group1-group100;
retain groupN 0 group1-group100;
categorized = 0;
* Search for the word or synonym in the existing groups;
if (groupN >= 1) then do;
do currentGroup = 1 to groupN;
if (index(groups[currentGroup], "#"||strip(word)||"#") and index(groups[currentGroup], "#"||strip(synonym)||"#") = 0) then do;
groups[currentGroup] = strip(groups[currentGroup])||strip(synonym)||"#";
categorized = 1;
end;
if (index(groups[currentGroup], "#"||strip(word)||"#") = 0 and index(groups[currentGroup], "#"||strip(synonym)||"#")) then do;
groups[currentGroup] = strip(groups[currentGroup])||strip(word)||"#";
categorized = 1;
end;
if (index(groups[currentGroup], "#"||strip(word)||"#") and index(groups[currentGroup], "#"||strip(synonym)||"#")) then do;
categorized = 1;
end;
end;
end;
* If the word and synonym were not found in the existing groups, create a new one;
if (categorized = 0) then do;
groups[groupN + 1] = "#"||strip(word)||"#"||strip(synonym)||"#";
groupN = groupN + 1;
end;
* Split the groups into unique key/value pairs;
if (_n_ = numObs) then do;
length key value $200;
keep key value;
do currentGroup = 1 to groupN;
if (not missing(groups[currentGroup])) then do;
key = scan(groups[currentGroup], 1, '#');
do j = 2 to countC(groups[currentGroup],'#');
value = scan(groups[currentGroup], j, '#');
if (not missing(value)) then do;
output;
end;
end;
end;
end;
end;
run;
proc sort data = groups;
by key;
run;
proc transpose data = groups out=result(drop = _:) prefix=synonym;
by key;
var value;
run;https://stackoverflow.com/questions/51729183
复制相似问题