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