代码之家  ›  专栏  ›  技术社区  ›  Andy De Vos

如何在CATS函数中一次添加所有列,而不是逐个写入

sas
  •  0
  • Andy De Vos  · 技术社区  · 12 月前

    我有以下数据:

    data WORK.TEMP_PTCAR_1;
     input ID $ COORDINATES $ 5-7;
     cards;
    ID1 1 5 
    ID1 4 8 
    ID1 8 7 
    ID2 3 4 
    ID2 9 2
    ;
    run;
    
    身份证件 协调
    ID1 1 5
    ID1 4 8
    ID1 8 7
    ID2 3 4
    ID2 9 2

    我想要这个:

    身份证件 协调
    ID1 管柱(1 5、4 8、8 7)
    ID2 管柱(3 4,9 2)

    我尝试使用以下代码行:

    PROC TRANSPOSE DATA=WORK.TEMP_PTCAR_1
        OUT=WORK.TEMP_PTCAR_2(LABEL="TEMP_PTCAR_2")
        PREFIX=XY
        NAME=Source
        LABEL=Label;
        BY ID ;
        VAR COORDINATES;
    RUN;
    
    Then I tried to create a custom column to show the linestring:
    
    ("LINESTRING(" || CATX(", ", t1.XY1,t1.XY2) || ")") AS Calculation
    
    But the problem is that I have to manually fill in all the columns (XY1, XY2, XY3,...) in the CATX function.
    

    还有别的办法吗?

    非常感谢您的帮助;)

    谢谢

    1 回复  |  直到 12 月前
        1
  •  0
  •   shaun_m    12 月前

    您可以使用带有BY组处理的DATA步骤来获得所需的结果。记得使用 length 语句,以确保目标变量足够长以包含结果。

    
    data TEMP_PTCAR_2;
        set TEMP_PTCAR_1;
        length new_coord $ 200;
        by ID;
        retain new_coord;
        if first.ID then new_coord = "LISTING("||coordinates;
        else new_coord = trim(new_coord)||","||coordinates;
        if last.ID then do;
            new_coord = trim(new_coord)||")";
            output;
        end;
        drop coordinates;
        rename new_coord = coordinates;
    run;