转置逗号分隔的字段 [英] Transposing Comma-delimited field

查看:18
本文介绍了转置逗号分隔的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的数据集,并且正在使用 SAS Enterprise Guide 6.3:

I have a dataset that looks like this, and am using SAS Enterprise Guide 6.3:

data have;
    input id state $;
    cards;
134 NC,NY,SC
145 AL,NC,NY,SC
;
run;

我有另一个数据集,该数据集在每个州的每个 id 都有多个指标,但我只需要为 have 数据集的第二列中列出的州提取数据.

I have another dataset that has several metrics for each id in every state, but I only need to pull the data for the states listed in the second column of the have dataset.

data complete;
    input id state $ metric;
cards;
134 AL 5
134 NC 4.3
134 NY 4
134 SC 5.5
145 AL 1.3
145 NC 1.3
145 NY 1.5
145 SC 1.1
177 AL 10
177 NC 74
177 NY 23
177 SC 33
;
run;

我想过使用 trnwrd 将逗号替换为 ', ' 并连接开始和结束引号以使列表成为字符列表,以便我可以使用 WHERE IN 语句.但是,我认为如果我能以某种方式将逗号分隔列表转换为这样的内容会更有帮助:

I thought about using trnwrd to replace the comma with ', ' and concatenating a beginning and ending quote to make the list a character list, so that I could use a WHERE IN statement. However, I think it would be more helpful if I could somehow transpose the comma separated list to something like this:

data have_mod;
        input id state $;
        cards;
    134 NC
    134 NY
    134 SC
    145 AL
    145 NC
    145 NY
    145 SC
    ;
    run;

然后我可以简单地将这个表连接到完整的数据表以获得我需要的子集(如下).

Then I could simply join this table to the complete data table to get the subset I need (below).

data want;
    input id state $ metric;
cards;
134 NC 4.3
134 NY 4
134 SC 5.5
145 AL 1.3
145 NC 1.3
145 NY 1.5
145 SC 1.1
;
run;  

有什么想法吗?谢谢.

推荐答案

我会完全按照你的建议去做并转换它 - 除非我是那样阅读的.

I'd do exactly what you propose and transpose it - except i'd read it in that way.

data have;
    infile datalines truncover dlm=', ';
    length state $2;
    input id @;   *read in the id for that line;
        do until (state='');   *keep reading in until state is missing = EOL;
            input state $ @;
            if not missing(state) then output;
        end;
    cards;
134 NC,NY,SC
145 AL,NC,NY,SC
;
run;

或者,您可以SCAN获取第一个状态码.

Alternately, you can SCAN for the first statecode.

data want_to_merge;
  set have;
  state_first = scan(state,1,',');  
run;

SCAN 相当于读取分隔文件的功能.

SCAN is the function equivalent of reading in a delimited file.

这篇关于转置逗号分隔的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆