【问题标题】:SAS find first value in column and get row numberSAS在列中找到第一个值并获取行号
【发布时间】:2019-03-04 18:09:19
【问题描述】:

我正在尝试导入和清理一些文件,其中第 1 列的第一行包含描述和列名。数据在包含“BEGINDATA”的行之后开始。 是否可以在 sas 表中搜索“BEGINDATA”并将行号分配给宏变量?在下一个数据步骤中,我可以使用 firstobs=macro 变量仅加载数据。

感谢您的任何提示!

【问题讨论】:

  • 文件是什么格式的?
  • 这是一个我已经导入 SAS 的 datfile。
  • 不确定“datfile”是什么,但如果您的源文件是简单的文本文件,例如 CSV 文件,那么读取它的数据步骤也可以忽略“BEGINDATA”之前的行。最好避免使用 PROC IMPORT 之类的工具来猜测文件中的内容。

标签: sas position


【解决方案1】:

当然!请参阅此示例。

/* Generate example data */
data have;
    do i = 1 to 10000;
        if(i = 100) then description = 'BEGINDATA';
            else call missing(description);

        value = rand('uniform');
        output;
    end;

    drop i;
run;

/* Get row where the data begins. Only keep the description variable 
   to reduce the size of the PDV */
data _null_;
    set have(keep=description);

    if(description = 'BEGINDATA') then do;
        call symputx('startrow', _N_, 'G');
        stop;
    end;
run;

/* Read from the data start row */
data want;
    set have(firstobs=&startrow.);
run;

【讨论】:

    【解决方案2】:

    在它已经“导入”之后,您无能为力。但是,如果您的源文件只是一个文本文件,那么读取它的数据步骤可以在读取数据的同一步骤中跳过前缀。

    data want;
      infile 'myfile.csv' dsd truncover ;
      input test :$20. @1 @;
      do while (test ne 'BEGINDATA');
         input ;
         input test :$20. @1 @;
      end;
      * Code to read the actual data lines ;
    run;
    

    【讨论】:

      猜你喜欢
      • 2017-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-14
      • 2021-12-17
      相关资源
      最近更新 更多