【问题标题】:How to use SAS to split a string into two variables如何使用 SAS 将字符串拆分为两个变量
【发布时间】:2013-12-28 15:25:50
【问题描述】:
  1. 我有一个数据集如下:

    country
    United States, Seattle
    United Kingdom, London
    

如何将国家/地区拆分为 SAS 中的数据,例如:

    country                  city
    United States           Seattle
    United Kingdom          London

【问题讨论】:

  • 顺便说一句,在谷歌上搜索这类问题的答案比在论坛上提问要快得多,也更有教育意义;)
  • 另一方面,当更多的人在论坛上提出这些问题时,情况就会变得更加真实......

标签: sas dataset


【解决方案1】:

使用以逗号作为分隔符的函数 SCAN()。

data test;
  set test;
  city=scan(country,2,',');
  country=scan(country,1,',');
run;

【讨论】:

    【解决方案2】:

    另一个选项,INFILE magic(谷歌这个主题的论文术语);对于从一个字符串解析多个变量和/或处理带引号的字段很有用,这样使用scan 会更有效。

    filename tempfile "c:\temp\test.txt";
    
    
    data have;
    input @1 country $50.;
    datalines;
    United States, Seattle
    United Kingdom, London
    ;;;;
    run;
    
    data want;
    set have;
    infile tempfile dlm=',' dsd;
    input @1 @@;
    _infile_=country;
    format newcountry city $50.;
    input newcountry $ city $ @@;
    run;
    

    tempfile 可以是任何文件(或者您在运行中创建的文件,其中包含任何字符以避免过早的 EOF)。

    【讨论】:

      猜你喜欢
      • 2022-01-05
      • 2020-12-27
      • 2022-01-20
      • 2021-12-18
      • 2020-04-11
      • 1970-01-01
      • 1970-01-01
      • 2021-12-11
      • 2016-04-06
      相关资源
      最近更新 更多