Help using Base SAS procedures

how the do loop work with %let

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 111
Accepted Solution

how the do loop work with %let

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;


Accepted Solutions
Solution
‎03-14-2015 04:37 AM
Respected Advisor
Posts: 3,899

Re: how the do loop work with %let

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


All Replies
Respected Advisor
Posts: 3,899

Re: how the do loop work with %let

%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;

Frequent Contributor
Posts: 111

Re: how the do loop work with %let

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.

Solution
‎03-14-2015 04:37 AM
Respected Advisor
Posts: 3,899

Re: how the do loop work with %let

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;

Super User
Super User
Posts: 6,502

Re: how the do loop work with %let

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;

Super User
Posts: 9,687

Re: how the do loop work with %let

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

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 267 views
  • 0 likes
  • 4 in conversation