BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
robby_beum
Quartz | Level 8

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()

1 ACCEPTED SOLUTION

Accepted Solutions
CTorres
Quartz | Level 8

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

5 REPLIES 5
CTorres
Quartz | Level 8

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

robby_beum
Quartz | Level 8

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

CTorres
Quartz | Level 8

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

RickAster
Obsidian | Level 7

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.

user24feb
Barite | Level 11

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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