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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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