首页 > 解决方案 > How to use cycles in sas macro data step

问题描述

I need to use cycles in a sas macro that writes a data step

I have a code that should work but it doesn't. How can i fix it?

%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;

I'd like to get the following output:

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;

标签: macrossassas-macro

解决方案


You are very close. You simply had an extra ; in your first loop.

You need to change:

data 
%do i=1 %to 3;
  _z%sysfunc(putn(%eval(&i),z2.)) ;
%end;
;

to:

data 
%do i=1 %to 3;
  _z%sysfunc(putn(%eval(&i),z2.)) 
%end;
;

Adding option mprint; to the beginning of your code would show you the code that was generated from your macro statement and helped you to debug it.


推荐阅读