DATA Step, Macro, Functions and more

Macro do loop with by statement

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 107
Accepted Solution

Macro do loop with by statement

Dear SAS users,

 

I am running the following code:

%macro do_all;
%do i=0 %to 1 %by 0.25;
options mprint symbolgen;

Data c_&i;
set individuals;
contractbypc=&i;
keep klantnummer treated year contractbypc;
run;

%end;
%mend do_all;
%do_all;

 I am getting the following error msg:

 

 

ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was:

.25

ERROR: The %BY value of the %DO I loop is invalid.

ERROR: The macro DO_ALL will stop executing.

 

I know its the %by part of the statement than is wrong, but I don't know how to fix it.

 

Thanks for your help.

 

K.


Accepted Solutions
Solution
Tuesday
Respected Advisor
Posts: 2,647

Re: Macro do loop with by statement

You can't use fractional or decimal arithmetic in a macro %DO statement. You can only use integers. Here is a solution to use non-integer values in a %DO statement: http://documentation.sas.com/?docsetId=mcrolref&docsetTarget=p02f7difkhfytsn1dzhaas0rdf4n.htm&docset...

--
Paige Miller

View solution in original post


All Replies
Solution
Tuesday
Respected Advisor
Posts: 2,647

Re: Macro do loop with by statement

You can't use fractional or decimal arithmetic in a macro %DO statement. You can only use integers. Here is a solution to use non-integer values in a %DO statement: http://documentation.sas.com/?docsetId=mcrolref&docsetTarget=p02f7difkhfytsn1dzhaas0rdf4n.htm&docset...

--
Paige Miller
Super User
Super User
Posts: 9,193

Re: Macro do loop with by statement

You can't call a dataset c_0.25 for example either.  Why do you want to split similar data out in the first place, it just makes any further code much harder to write and takes more disk space?

I suppose you could do:

data _null_;
  do i=0 to 1 by 0.25;
    call execute(cats('data c_',tranwrd(put(i,best.),".","_"),'; set individuals; where contractbypc=',put(i,best.),'; run;'));
  end;
run;
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 82 views
  • 0 likes
  • 3 in conversation