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

Good day all.
i have 12macro variables. they are in the format.
&month_nm1='Jan 2014$' to
&month_nm12='Dec 2014$'
now i want the out put like
&mname1=Jan 2014
&mname12=Dec 2014.
and here &mon_tot value is 12.
SO i write do loop is it right or not.
in log its showing some error. and not executing the condition.

%macro month;
%do i=1 %to &mon_tot %by 1;
%let mname&i=%sysfunc(compress(&&month_nm&i,'$', 'p'));
%end;
%mend month;
%month;

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

It works with the sample code. If your actual code is different then may be you post it (especially the section where you create &month_nm<n>). Post the error log (using options mprint) as well because this will be more helpful to us than just you writing "it's not working".

%let month_nm1='Jan 2014$';

%let month_nm2='Feb 2014$';

%let mon_tot=2;

%macro month;

  %do i=1 %to &mon_tot %by 1;

    %global mname&i;

    %let mname&i=%sysfunc(compress(&&month_nm&i,%str($%')));

    %put mname&i: &&mname&i;

  %end;

%mend month;

%month;

%put mname1: &mname1;

%put mname2: &mname2;

View solution in original post

5 REPLIES 5
Patrick
Opal | Level 21

%let month_nm1='Jan 2014$';

%let month_nm2='Feb 2014$';

%let mon_tot=2;

%macro month;

  %do i=1 %to &mon_tot %by 1;

    %let mname&i=%sysfunc(compress(&&month_nm&i,%str($%')));

    %put mname&i: &&mname&i;

  %end;

%mend month;

%month;

Ravikumarkummari
Quartz | Level 8

sorry all,

its not mname1 its &mname1 to &mname12

Just now edited.

In the open code the variables are not working.

%put &mname1;

%put  &mname2;

how the macro variables are working open code.

i used %global statement within the macro but the variables are not working in open code.

Patrick
Opal | Level 21

It works with the sample code. If your actual code is different then may be you post it (especially the section where you create &month_nm<n>). Post the error log (using options mprint) as well because this will be more helpful to us than just you writing "it's not working".

%let month_nm1='Jan 2014$';

%let month_nm2='Feb 2014$';

%let mon_tot=2;

%macro month;

  %do i=1 %to &mon_tot %by 1;

    %global mname&i;

    %let mname&i=%sysfunc(compress(&&month_nm&i,%str($%')));

    %put mname&i: &&mname&i;

  %end;

%mend month;

%month;

%put mname1: &mname1;

%put mname2: &mname2;

Tom
Super User Tom
Super User

You could use %SCAN() if your data is in that format.  Just use ' and $ as the delimiters.  Note you can code the delimiter list as '$' to avoid unbalanced quotes and the need for macro quoting.  It doesn't matter if an individual delimiter appears more than once in the list.

%let sheet1='Jan 2014$';

%let sheet2='Feb 2014$';

%let sheetN=2;

%let month1 = Before;

%let month2 = Before;

%macro sheet2month;

  %do i=1 %to &sheetn ;

    %let month&i=%scan(&&sheet&i,1,'$') ;

  %end;

%mend sheet2month;

%sheet2month;

%put &=sheet1 &=month1;

%put &=sheet2 &=month2;

Ksharp
Super User

Your original code looks good . except don't put quote in macro function .

%let month_nm1='Jan 2014$';

%let month_nm2='Feb 2014$';

%let mon_tot=2;

%macro month;

%do i=1 %to &mon_tot %by 1;

%let mname&i=%sysfunc(compress(&&month_nm&i,$, p));

%put &&mname&i ;

%end;

%mend month;

%month

Xia Keshan

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 5 replies
  • 6412 views
  • 0 likes
  • 4 in conversation