Assign values to array in do loop

Reply
N/A
Posts: 1

Assign values to array in do loop

Upfront... just want to say thanks for the help...

I have the following code snipet for assigning values from a dataset to an array... I get an error message about converting numeric values to char values. I have not been able to find a solution to this. Would greatly appreciate any help...

---------------------------------------------------------------------------

%macro DefineInputArray;
      ARRAY BYLINE(48) $16.  BYLINE1 - BYLINE50;
%mend;

data sims;

set data_inputs;
%DefineInputArray;

format iter BEST.;
do i=1 to 50;
  %let iter = 1000+(i-1)*1000;
  BYLINE(i) = iVALUE&iter .;
end;


run;

---------------------------------------------------------------------------

NOTE: Numeric values have been converted to character values at the places given by:      (Line)Smiley SadColumn).

---------------------------------------------------------------------------

Contributor
Posts: 23

Re: Assign values to array in do loop

Try using %eval() with the %let statement:

  %let iter = %eval(1000+(i-1)*1000);

Respected Advisor
Posts: 4,137

Re: Assign values to array in do loop

You will have a timing issue when using a macro %let statement in a data step. The %let statement will get executed before the data step so it won't be part of the data step iteration and you also can't pass a SAS data step variable to the %let statement. There your "i" will simply be interpreted as the character 'i'.

Not sure why you need a macro at all but as this is a code snippet reality might look different and there are good reasons for a macro. Below should make your code snippet work:

%macro DefineInputArray;

      ARRAY BYLINE(50) $16.  BYLINE1 - BYLINE50;

   array iVal (50)

   %do i=1 %to 50;

     %let iter = %eval(1000+(&i-1)*1000);

  iVALUE&iter

   %end;

    %str(Smiley Wink

%mend;

data sims;

  set data_inputs;

  %DefineInputArray;

  do i=1 to dim(BYLINE);

   BYLINE = iVALUE;

  end;

run;

Ask a Question
Discussion stats
  • 2 replies
  • 178 views
  • 0 likes
  • 3 in conversation