在宏执行循环中输出到数据集

时间:2013-01-29 02:18:50

标签: sas

这是我写的宏,我想查看给定的数据集,并将每个字段(列)名称写为信息数据集的观察结果。

这是我所拥有的

%macro go_macro(data);


/*get the list of field names from the dictionary*/
proc sql noprint;

    select distinct name into :columns separated by ' '
    from dictionary.columns
    where memname = "%upcase(%scan(&data, -1))" and libname = "%upcase(%scan(&data, 1))" and type = "char"
;

quit;

/*now loop through this list of fieldnames*/
%let var_no = 1; 
%let var = %scan(&columns, &var_no, ' '); 

%do %while (&var ne);

                 /*here is where I want to write the fieldname to an observation*/
    fname = "&var"; 
                 output; 


%let var_no = %eval(&var_no +1);
%let var = %scan(&columns, &var_no, ' '); 
%end;

%mend;


/*execute the code*/
data bdqa.accounts_info; 
%go_macro(braw.accounts)
run;

这给了我

[MPRINT] Parsing Base DataServer
/* 0005 */      fname = "SORT_CODE"; 
/* 0006 */          output; 
/* 0009 */      fname = "BANK_NAME"; 
/* 0010 */          output; 
/* 0013 */      fname = "CREDIT_CARD"; 
/* 0014 */          output; 
/* 0017 */      fname = "PRIMARY_ACCT_HOLDER"; 
/* 0018 */          output; 
/* 0021 */      fname = "account_close_date"; 
/* 0022 */          output;
/* 0023 */      run;


    ERROR: Parsing exception - aborting
ERROR: DS-00274 : Could not parse base DataServer code: Encountered " <ALPHANUM> "fname "" at line 5, column 9.
Was expecting one of:
    <EOF> 
    ";" ...
    "*" ...
    "data" ...
    "proc" ...
    (and 9 more)
    while

,而

data mytest;

    do i = 1 to 5; 
    fname = 'hello world';
    output; 
    end;
    keep fname; 
run;

完全合法。

以下代码

%macro char_freqs(data=);

/*get the different variables*/
proc sql noprint;

select distinct name into :columns separated by ' '
from dictionary.columns
where memname = "%upcase(%scan(&data, -1))" and libname = "%upcase(%scan(&data, 1))" and type = "char"
;

quit;

/*now get the distinct values for each of the variables*/
%let var_no = 1; 
%let var = %scan(&columns, &var_no, ' '); 

%do %while (&var ne);

    proc freq data=&data; 
    tables &var/out=&var._freq; 
    run;    

%let var_no = %eval(&var_no +1);
%let var = %scan(&columns, &var_no, ' '); 
%end;

%mend;


%char_freqs(data=macros.businesses)

同样有效 - 允许使用PROC FREQ。

1 个答案:

答案 0 :(得分:3)

问题是你有这样的事情:

data bdqa.accounts_info; 
%go_macro(braw.accounts)
run;

- &GT;

data bdqa.accounts_info; 
proc sql;
... select stuff ...;
quit;
fname = "stuff"; ...
run;

你需要:

proc sql;
select stuff;
quit;


data bdqa.accounts_info; 
 fname = "stuff";
 ...
run;

您需要从宏中删除PROC SQL - 您可以在宏之外创建宏变量。老实说,你根本不应该使用宏 - 你可以做以下两件事之一: a)直接从DICTIONARY.COLUMNS

创建表
proc sql;
create table bdqa.accounts_info as select name as fname from dictionary.columns where ... ;
quit;

b)在datastep

中创建它
data bdqa.accounts_info;
__data = "&var_no";
do ... ;
run;

(do循环基本上与宏中的%do循环相同)

相关问题