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

Hi all,

I would like to have something like this:

do i=1 to 12;

      month&i = 5 + i - 1;

      output;

end;

which should make the variables

month1 = 5

month2 = 6

...

month12 = 16

However, writing month&i doesn't work.

Any ideas?

Thanks in advance,

Marco

1 ACCEPTED SOLUTION
7 REPLIES 7
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

The & (ampersand) is for macro processing.  The easiest way to change it to use & is to wrap a macro around it:

%tmp ();

     data a;

          %do I=1 %to 10;

               b=&I.;

          %end;

     run;

%mend tmp()

%tmp;

However I am not saying that is the best solution or the way to go, only answering the specific item.  If I wanted to do this type of thing then I would generate the code:

data _null_;

    call execute('data want;');

    do i=1 to 12;

         call execute('month'||strip(put(I,best.))||'=5+'||strip(put(I,best.))||'-1;

                                                 output;');

     end;

     call execute('run;');

run;

This will generate a dataset with the 12 month statements and execute it.

Lupacante
Calcite | Level 5

Thanks, that works.

Now I would like to make variables month1 - month12 available for use later in the code. I've come up with this, which works:

call symputx('month1',month1);

call symputx('month2',month2);

...

call symputx('month12',month12);

But is there any way to put that as a loop?

Thanks

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Yes, with KurtBremsers code you can just put the call symputx after the months{I} statement.  However, this goes back to my post and generating the code.  Why go to all the bother of creating macro varibles when you can just expand the code generator to generate the later code you also need, e.g. if I want to create a dataset with the month name:

data _null_;

  do i=1 to 12;

    call execute('data month'||strip(put(5+I,best.))||';

                    do some other code;

                  run;');

     end;

run;

You really see the benefits on large programs, when all the generator needs is some parameterization.

Lupacante
Calcite | Level 5

How, just by adding

call symputx('months(i)',months(i);

?

Ok, I'll look into the call execute function, thanks again.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Been a while since I use symput but:

call symput('months'||strip(put(I,best.)),months{I});

You need to use curly braces for arrays.  I hope the concatenation works in symput like that :smileyshocked:)

Kurt_Bremser
Super User

Just add

call symput('month'!!strip(put(i,best.)),strip(put(months{i},best.)));

in the loop.

I just added some small things to RW9's suggestion to avoid NOTE's in the log and any unwanted blanks in the macro vars.

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 2412 views
  • 7 likes
  • 3 in conversation