0
votes

I am currently attempting to create a macro that is setting parts of a string to a list of global variables where the parts of the string are separated by "-" so for example, 672-46-246 would make three global variables, name2, name2, and name4, which are called 672, 46, 246 respectively. There's also a name1 which is the global variable of the entire string. But anyways I got the ability to get the entire string to a global variable but I'm having trouble splitting it up. There may be other ways to do this but what I'm trying to do is doing a for loop to how many "splits" there are, i.e. "-" and then substringing it by those splits. Right now I'm trying to find out how many "splits" there are but the code is returning

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

Not sure what else to include here in the summary.

Code below

%GLOBAL C;
%LET C=1;
DATA _NULL_;
    SET T_&scope.;
    %DO I = 1 %TO &sqlobs.;
        %GLOBAL name&C.;
        CALL SYMPUT("name&C.", COL&I.);
        %Counter(C);
        CALL SYMPUT("splits", COUNTC(COL&I., "-"));
        %DO J = 0 %TO &splits.;
            %Counter(C);
        %END;
    %END;
RUN;

WARNING: Apparent symbolic reference SPLITS not resolved. ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was: &splits.

1
Please show clearly what your inputs are and what you want as outputs. It is really not clear what you are trying to do. If you are starting with macro variables show %LET statements to assign the initial values. If your outputs are macro variables then show %LET statements that demonstrate the expected output for the given input. If input or output is a dataset then show a data step that creates the dataset.Tom
It is usually not very helpful to set a list of macro variables. Why not just leave the values in the single macro variable as a delimited string and pull out each value as you need it?Tom

1 Answers

2
votes

Not sure what the question is but the reason for your error is that you are trying to use macro variable SPLITS before you have given it a value. The macro processor runs first and then the resulting code is compiled and run by SAS. So your %do j=0 %to &splits; macro statement is evaluated BEFORE your data step starts to run and so definitely before the CALL SYMPUT() statement can run.

If you have a list of values in a macro variable use %SCAN() to pick out the individual values.

%let list= 672-46-246 ;
%do i=1 %to %sysfunc(countw(&list,-));
  %let next=%scan(&list,&i,-);
  ...
%end;