DATA Step, Macro, Functions and more

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

Accepted Solution Solved
Reply
Contributor
Posts: 72
Accepted Solution

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

I am getting this error on the following code. Even though numDim is clearly a numeric value. When I replace "%do i = 1 %to numDim" with "%do i = 1 %to 5" it works. Any ideas on why this error is occuring and how to fix it?

ERROR: A character operand was found in the %EVAL function or %IF condition

       where a numeric operand is required. The condition was: numDim

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

ERROR: The macro CREATEDIMS will stop executing.

data test;
   input idno 1-2 l_name $ 5-9 diag $ 12-30;
   cards;
10  Smith  123:199:256:277:600
;
run;

data test_1;
   length numDim 8.;
   set test;
   numDim=(countc(diag, ':')+1);
run;

%macro createDims();
   data test_2;
      set test_1;

      %do i = 1 %to numDim;
         Variable_a&i = scan(diag, &i, ':');
         diag_code=Variable_a&i.;

         drop diag Variable_a&i;

         output;
      %end;
   run;
%mend createDims;
%createDims()


Accepted Solutions
Solution
‎01-08-2015 12:16 PM
Regular Contributor
Posts: 180

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

Now I understand.

You do not need macro languaje to solve the problem:

data test;
   input idno 1-2 l_name $ 5-9 diag $ 12-30;
   cards;
10  Smith  123:199:256:277:600
11  Jones  222:111
12  Jeddy  555:758:987:111
;
run;

data test3;
   length numDim 8.;
   set test;
   numDim=(countc(diag, ':')+1);
   do i = 1 to numDim;
      diag_code=scan(diag, i, ':');
      output;
   end;
   drop diag i;
run;

Thanks RiskAster for your explanation.

CTorres

View solution in original post


All Replies
Regular Contributor
Posts: 180

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

Try this:

data test;
   input idno 1-2 l_name $ 5-9 diag $ 12-30;
   cards;
10  Smith  123:199:256:277:600
;
run;

data test_1;
   length numDim 8.;
   set test;
   numDim=(countc(diag, ':')+1);

   call symputx('numDim',numDim);
run;

%macro createDims();
   data test_2;
      set test_1;

      %do i = 1 %to &numDim;
         Variable_a&i = scan(diag, &i, ':');
         diag_code=Variable_a&i.;

         drop diag Variable_a&i;

         output;
      %end;
   run;
%mend createDims;
%createDims()

CTorres

Contributor
Posts: 72

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

Yes, that works with 1 record. Sorry, I should have included more records. I added the numDim to each line of the the dataset because I want to read it and use it for each line of the dataset


10  Smith  123:199:256:277:600
11  Jones  222:111
12  Jeddy  555:758:987:111

data test_1;
   length numDim 8.;
   set test;
   numDim=(countc(diag, ':')+1);
run;

should produce:

numDim  idno   l_name  diag
5             10      Smith     123:199:256:277:600
2             10      Jones     222:111
4             10      Leddy     555:758:987:111

Solution
‎01-08-2015 12:16 PM
Regular Contributor
Posts: 180

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

Now I understand.

You do not need macro languaje to solve the problem:

data test;
   input idno 1-2 l_name $ 5-9 diag $ 12-30;
   cards;
10  Smith  123:199:256:277:600
11  Jones  222:111
12  Jeddy  555:758:987:111
;
run;

data test3;
   length numDim 8.;
   set test;
   numDim=(countc(diag, ':')+1);
   do i = 1 to numDim;
      diag_code=scan(diag, i, ':');
      output;
   end;
   drop diag i;
run;

Thanks RiskAster for your explanation.

CTorres

Contributor
Posts: 22

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

I’ll try to add an explanation in case readers are wondering what’s going on in the solution suggested by CTorres. A %DO loop can be used to generate a block of SAS code repeatedly with different index values. However, since %DO is a macro statement, any variables in it would have to be macro variables. There are various ways to create macro variables, and the CALL SYMPUTX routine is a good approach if you want to create a macro variable from a a value in a data step. Then, once the macro variable exists, a macro variable reference has to begin with an ampersand — that’s what marks it as a macro variable. As originally presented, the %DO statement is looking at the text “numDim” in the %DO statement, rather than a variable. The %DO statement uses the %EVAL function to get the numeric value of its %TO argument, and this is why the error message refers to the %EVAL function.

Super Contributor
Posts: 336

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

I agree with CTorres. You don#t even need the numDim variable:

data test;
   input idno 1-2 l_name $ 5-9 diag $ 12-30;
   cards;
10  Smith  123:199:256:277:600
;
run;

Data Test_2 ;
  Set Test;
  Do i=1 To CountC(Diag,':')+1 By 1;
    Diag_Code=Scan(Diag,i,':');
Output;
  End;
Run;

☑ This topic is SOLVED.

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

Discussion stats
  • 5 replies
  • 1385 views
  • 3 likes
  • 4 in conversation