Sas:如何使用宏填充多个txt?

Sas:如何使用宏填充多个txt?,sas,sas-macro,Sas,Sas Macro,我需要宏有一个参数,这是文本文件的路径 这就是我现在拥有的。我有6个txt,名称=NY2012.txt NY2013.txt NY2014.txt…等等 %macro data ; %let list= 2012 2013 2014 2015 2016 2017; data allData; delete; (what should I put here?) run; %do i=1 %to 6; %let currItem

我需要宏有一个参数,这是文本文件的路径

这就是我现在拥有的。我有6个txt,名称=NY2012.txt NY2013.txt NY2014.txt…等等


    %macro data ;

    %let list= 2012 2013 2014 2015 2016 2017;


    data allData;
    delete;  (what should I put here?)
    run;


    %do i=1 %to 6;
    %let currItem = %scan(&list, &i);

    filename f&list "/folders/myfolders/NY&list.txt"; (should this be here?)
    data currentData;
    x = &currItem;

    infile f&list  truncover;
    input value $ 1-20;
    retain year;
    if _n_=1 then year=year_1;

    run;

    * Keeping adding things to the cumulative data set;
    data allData;
    set allData currentData;
    run;
    %end;

    %mend data;

    %data;



然后,我应该得到一个每年的数据集和一个包含所有年份的大型数据集。我该如何解决这个问题?谢谢。

您应该在宏变量中按元素进行循环,并使用列表中的扫描元素,而不是循环中的元素列表。因此,代码如下所示:

%let list=2017 2018 2019;

%macro data(tlist) ; %macro d; %mend d;

    %do i=1 %to %sysfunc(countw(&tlist,%str( )));
    %let currItem = %scan(&tlist, &i, %str( ));

    filename f&currItem. "/folders/myfolders/NY&currItem..txt";
    data currentData&currItem;
    x = &currItem;

    infile f&currItem  truncover;
    input value $ 1-20;
    retain year;
    if _n_=1 then year=year_1;

    run;

    * Keeping adding things to the cumulative data set;
    data allData;
    set allData currentData&currItem;
    run;
    filename f&currItem. clear;
    %end;

%mend data;

%data;

%data(&list);

您应该在宏变量中按元素进行循环,并使用列表中的扫描元素,但不使用循环中的元素列表。因此,代码如下所示:

%let list=2017 2018 2019;

%macro data(tlist) ; %macro d; %mend d;

    %do i=1 %to %sysfunc(countw(&tlist,%str( )));
    %let currItem = %scan(&tlist, &i, %str( ));

    filename f&currItem. "/folders/myfolders/NY&currItem..txt";
    data currentData&currItem;
    x = &currItem;

    infile f&currItem  truncover;
    input value $ 1-20;
    retain year;
    if _n_=1 then year=year_1;

    run;

    * Keeping adding things to the cumulative data set;
    data allData;
    set allData currentData&currItem;
    run;
    filename f&currItem. clear;
    %end;

%mend data;

%data;

%data(&list);

内嵌上的简单通配符就足够了

在这种情况下????表示任意4个字符。您也可以使用NY*.txt


内嵌上的简单通配符就足够了

在这种情况下????表示任意4个字符。您也可以使用NY*.txt