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()
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
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
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
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
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.
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.