【问题标题】:Identify related value pairs in datasets with sas使用 sas 识别数据集中的相关值对
【发布时间】:2018-08-07 14:25:51
【问题描述】:

我有一个包含单词同义词信息的数据集(很多行) 数据集的简要示例如下所示。 呈现每个单词的同义词信息。

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

我尝试了inner join的冗余步骤,但似乎是很多不必要的过程。

【问题讨论】:

  • 您有 SAS/OR 许可吗?它有许多过程可以从您的数据类型中查找连接的子图。
  • SAS - grouping pairs的可能重复

标签: join sas dataset


【解决方案1】:

我很难在 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;

【讨论】:

    猜你喜欢
    • 2023-04-02
    • 1970-01-01
    • 2015-10-15
    • 2018-04-18
    • 2017-10-20
    • 2018-03-22
    • 1970-01-01
    • 1970-01-01
    • 2019-03-21
    相关资源
    最近更新 更多