将变量循环写入数据集

时间:2019-07-23 11:32:31

标签: sas

我想在循环中为变量分配一个字符串,并在每次迭代时将变量写出到数据集。

这是打印出每个变量的代码

%macro t_size(inlib=,inds=);

   %let one_gig = 5000;

   proc sql noprint;
        select ceil((nobs*obslen)/&one_gig) into :tsize 
        from sashelp.vtable where libname=upcase("&inlib") and memname=upcase("&inds");
   quit;


   %let no_of_tables=%eval(%sysfunc(int(&tsize)));



   %if (&tsize gt 1) %then
      %do i = 1 %to &no_of_tables;
         %put &inds._&i.;
      %end;
   %else
      %do;
         %put &inds.;
      %end;

%mend;

%t_size(inlib=SASHELP,inds=SHOES);
run;

这将产生所需的输出:

SHOES_1
SHOES_2
SHOES_3
SHOES_4
SHOES_5
SHOES_6
SHOES_7

我不想将变量打印到日志中,而是将它们写入新的空数据集。

3 个答案:

答案 0 :(得分:1)

解决方案:

%macro t_size(inlib=,inds=);

   %let one_gig = 5000;

   proc sql noprint;
        select ceil((nobs*obslen)/&one_gig) into :tsize 
        from sashelp.vtable where libname=upcase("&inlib") and memname=upcase("&inds");
   quit;


   %let no_of_tables=%eval(%sysfunc(int(&tsize)));


   data temp;
   length temp $100;
   %if (&tsize gt 1) %then
      %do i = 1 %to &no_of_tables;
         temp= "&inds._&i.";
         output;
      %end;
   %else
      %do;
         temp= "&inds.";
         output;
      %end;
   run;

%mend;

%t_size(inlib=SASHELP,inds=SHOES);
run;

只需添加名为temp的数据步骤,即可在变量temp中输入。

输出:

+---------+
|  temp   |
+---------+
| SHOES_1 |
| SHOES_2 |
| SHOES_3 |
| SHOES_4 |
| SHOES_5 |
| SHOES_6 |
| SHOES_7 |
+---------+

答案 1 :(得分:1)

您似乎正在尝试将数据集 FOO 分为N个 one_gig FOO_1 FOO_N 。您的第一步 也似乎是在创建FOO目标表名称。在DATA步骤中计算拆分名称将保存计算的名称。

示例:

%macro make_split_names(data=, out=split_names, splitsize=5000);

%local lib mem;
%let syslast = &data;
%let lib = %scan(&data,1,.);
%let mem = %scan(&data,2,.);

data parts;
  ds = open ('sashelp.cars');
  nobs = attrn(ds, 'NOBS');
  lrecl = attrn(ds, 'LRECL');
  ds = close(ds);

  do n = 1 to ceil ( nobs * lrecl / &splitsize );
    name = catx("_", "&mem", n);
    OUTPUT;
  end;

  keep name;
run;
%mend;


%make_split_names (data=sashelp.cars)

答案 2 :(得分:1)

如果需要数据集,则用数据步骤替换最后一个宏逻辑块。

data member_list ;
   length memname $32 ;
   if &no_of_tables > 1 then do i=1 to &no_of_tables;
     memname=catx('_',"&inds",i);
     output;
   end;
   else do;
     memname="&inds";
     output;
   end;
   keep memname;
run;