如何在 sas 宏数据步骤中使用循环
How to use cycles in sas macro data step
我需要在写入数据步骤的 sas 宏中使用循环
我有一个代码应该可以工作但没有。我该如何解决?
%macro ci;
data
%do i=1 %to 3;
_z%sysfunc(putn(%eval(&i),z2.)) ;
%end;
;
set _06;
%do i=1 %to 3;
if num="%sysfunc(putn(%eval(&i),z2.))" then output _z%sysfunc(putn(%eval(&i),z2.));
%end;
run;
%mend;
%ci;
我想要得到以下输出:
data
_z01
_z02
_z03;
set _06 ;
if num="01" then output _z01;
if num="02" then output _z02;
if num="03" then output _z03;
run;
如果您使用更简单的方法而不是使用将数字转换为字符,那怎么样
data _06;
num='01';
output;
num='02';
output;
num='03';
output;
run;
%macro ci;
data
%do i=1 %to 3;
_z0&i
%end;
;
set _06;
%do i=1 %to 3;
if num="0&i" then output _z0&i;
%end;
run;
%mend;
%ci;
你们很亲近。您只是在第一个循环中多了一个 ;
。
您需要更改:
data
%do i=1 %to 3;
_z%sysfunc(putn(%eval(&i),z2.)) ;
%end;
;
至:
data
%do i=1 %to 3;
_z%sysfunc(putn(%eval(&i),z2.))
%end;
;
将 option mprint;
添加到代码的开头将向您显示从宏语句生成的代码并帮助您调试它。
我需要在写入数据步骤的 sas 宏中使用循环
我有一个代码应该可以工作但没有。我该如何解决?
%macro ci;
data
%do i=1 %to 3;
_z%sysfunc(putn(%eval(&i),z2.)) ;
%end;
;
set _06;
%do i=1 %to 3;
if num="%sysfunc(putn(%eval(&i),z2.))" then output _z%sysfunc(putn(%eval(&i),z2.));
%end;
run;
%mend;
%ci;
我想要得到以下输出:
data
_z01
_z02
_z03;
set _06 ;
if num="01" then output _z01;
if num="02" then output _z02;
if num="03" then output _z03;
run;
如果您使用更简单的方法而不是使用将数字转换为字符,那怎么样
data _06;
num='01';
output;
num='02';
output;
num='03';
output;
run;
%macro ci;
data
%do i=1 %to 3;
_z0&i
%end;
;
set _06;
%do i=1 %to 3;
if num="0&i" then output _z0&i;
%end;
run;
%mend;
%ci;
你们很亲近。您只是在第一个循环中多了一个 ;
。
您需要更改:
data
%do i=1 %to 3;
_z%sysfunc(putn(%eval(&i),z2.)) ;
%end;
;
至:
data
%do i=1 %to 3;
_z%sysfunc(putn(%eval(&i),z2.))
%end;
;
将 option mprint;
添加到代码的开头将向您显示从宏语句生成的代码并帮助您调试它。