2015-06-04 56 views
1

我上具有以下结构的数据集工作:SAS MACRO循环

Color Apple Orange Grape Avocado Blueberry 
Yellow 1  .  .  .  . 
Orange .  1  .  .  . 
Purple .  .  1  .  1 

我想写每个果型创建表,选择所有的颜色(行)的宏有一个值为1.例如,苹果TBL_APPLE的表格将有如下4行:

目前我正在考虑循环遍历行和列。作为弗里斯特一步,我打开所有的行和列变量引入宏:

/*rows*/ 
    proc sql noprint; 
    select count(*) into :Nobs 
    from work.fruit; 
    select Color into :Attr1-:Attr%left(&Nobs) 
    from work.fruit;quit; 

    /*columns*/ 
    proc contents data=work.fruit out=contents noprint; run; 
    %let n=&sqlobs; 
    proc sql; select name into :fruit1-fruit%left(&n) from contents; quit; 

    %macro fruit; 
    %do i=1 %to &NObs; 
    %do j=1 %to &n; 
    proc sql; 
    create table tlb_&&fruit&j as 
    select * 
    from work.fruit 
    where &n = &n; 
    quit; 
    %end; 
    %end; 
    %mend fruit; 
    %fruit; 

回答

0

我会首先定义执行要重复的简单任务的宏:

%macro fruitds(fruit); 
data &fruit.(keep=color); 
    set fruit; 
    where &fruit eq 1; 
run; 
%mend fruitds; 

然后使用数据步骤从sashelp.vcolumnscall execute为每列名的宏不Color

data _null; 
    set sashelp.vcolumn; 
    call execute(cats('%fruitds(',name,')')); 
    where libname eq 'WORK' 
    and memname eq 'FRUIT' 
    and name ne 'Color'; 
run; 
1

不知道读书的列名,如果它是什么你想要的,但我的理解这个问题可能 被如下简化:

%macro fruit(type);  
    data &type ; 
    set dataset; 
    where &type = 1; 
    run; 
%mend fruit; 
0
%color_fruit; 
proc sql; 
     select name into:fruit_name separated by ' ' from dictionary.columns 
     where libname='WORK' and memname='FRUIT' and upcase(name)^='COLOR'; 
quit; 
%let nums_fruit=%sysfunc(countw(&fruit_name)); 
%do i=1 %to &nums_fruit; 
    %let fruit=%scan(&fruit_name,&i,%str()); 
    data tab_&fruit; 
      set fruit(keep=color &fruit); 
      if &fruit=1 then output; 
     run; 
%end; 
run; 
%mend;