首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用sas识别数据集中的相关值对

使用sas识别数据集中的相关值对
EN

Stack Overflow用户
提问于 2018-08-07 22:25:51
回答 1查看 89关注 0票数 2

我有一个包含单词同义词信息(多行)的数据集,下面是数据集的简要示例。给出了每个单词的同义词信息。

代码语言:javascript
复制
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代码后,我想要一个如下所示形式的数据集。

代码语言:javascript
复制
Word Synonym1 Synonym2 Synonym3
C01  C02      C05      C06
C03  C04
C11  C12

我尝试了内部连接的冗余步骤,但似乎有很多不必要的过程。

EN

回答 1

Stack Overflow用户

发布于 2018-08-07 23:56:07

我很难在SAS中找到一个好的解决方案(在其他语言中,这更容易解决)。下面的方法不是很好,因为它试图将所有组写入单个变量中,如果您有很多记录,这个变量很快就会用完。另外,它依赖于'#‘作为分隔符。如果您的单词可以包含此字符,您可能希望将其更改为不同的字符。

代码语言:javascript
复制
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;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51729183

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档