Learning SAS? Welcome to the exclusive online community for all SAS learners.

Create serially numbered macro variables

Reply
N/A
Posts: 1

Create serially numbered macro variables

Hello,

during my data step i would like to create macro variables (named "macro_variable_X") every time the column A of a row contains the value "mv". These macro variables should be serially numbered (X stands for the number).

(1) At the moment i have a problem concatenating the string "macro_variable_" and the number "X":

x=1;

  call symput (cat('m_column_', 'x'), someValue);

=> Does not create a macro variable called: "m_column_1" as it is supposed to.

(2) The next question is, how to increment my counter variable x.

I tried to define the counter variable before the datastep like this:

%LET x=0;

and increment it in my do loop like this:

if (A="mv") then do;

    call symput ('x', symget('x')+1);

    call symput (cat('m_column_', symget('x')), someValue);

end;

=> throws this ERROR: Symbolic variable name M_COLUMN_ 1 must contain only letters, digits, and underscores.

I would be grateful for any help :smileyconfused:

Super User
Super User
Posts: 7,039

Re: Create serially numbered macro variables

Posted in reply to Daniel_sasbeginner

CATS() will strip the spaces before concatenation.

Also use CALL SYMPUTX to strip the spaces from the value being stored into the macro variable.

You might also want to use CATX('_',...) instead of CATS() to have it insert the underscore for you.

Super User
Posts: 19,770

Re: Create serially numbered macro variables

Posted in reply to Daniel_sasbeginner

Why use a macro variable for X?

Increment X every time the A='mv' is true:

data _null_;

set have;

retain x 0;

if (A="mv") then do;

    x+1;

    call symputx(cat('m_column_',put(x, 3. -l)), someValue);

end;

run;

Ask a Question
Discussion stats
  • 2 replies
  • 292 views
  • 6 likes
  • 3 in conversation