Hello
I want to create multiple macro variables based on data set that contain 2 colums:
column 1 is Macro var name
Column 2 is the value that the macro var will get
for example:
Macro var M0 will get value 2309
Macro var M1 will get value 2308
and so on
What is the way to do it please?
I think that I did it well but when I write %put &&m&n..; to see the last macro var then I get warning and cannot see its value.Why???
WARNING: Apparent symbolic reference M not resolved.
26 %put &&m&n..;
&m..
%let startMon=2012;
%let LastMon=2309;
data _null_;
date_start=mdy(mod(&startMon,100),1,floor(&startMon/100));
date_end=mdy(mod(&LastMon,100),1,floor(&LastMon/100));
n = intck('month',date_start,date_end);
call symputx('n',put(counter,best.));
call symputx('date_start',put(date_start,best.));
call symputx('date_end',put(date_end,best.));
format date_start date_end date9.;
run;
data want_month;
date=&date_start.;
end_date=&date_end.;
YYMM=input(put(date,yymmn4.),best.);
format date end_date date9.;
do while (date<=end_date);
output;
date=intnx('month', date, 1, 's');
YYMM=input(put(date,yymmn4.),best.);
end;
format date YYMMN4.;
drop date end_date;
run;
proc sort data=want_month;
by descending YYMM ;
Run;
DATA want_month2(DROP=seq);
SET want_month;
seq = _N_-1;
Macro_Var_Name=CATS("M",seq);
RUN;
data _null_;
set want_month2;
call symputx(Macro_Var_Name,YYMM);
run;
%put &M0;
%put &M1;
%put &M33;
%put &&m&n..;/******Why WARNING: Apparent symbolic reference M not resolved??????***/
Remove the double dots after the macro variable reference.
%put &&m&n; doesn't work because your macro variable &n doesn't contain a number but a fullstop (="missing").
If you look at your first data step you'll see why that happens
I guess what you intended to do but then missed during development was to use variable N instead of COUNTER to populate macro variable &n
If I were you I would use MacroArray package and do it like this:
Generate data:
/* input data */
%let startMon=2012;
%let LastMon=2309;
data have;
n=-1;
do yymm=&startMon. to &LastMon.;
if input(put(yymm,z4.)!!"01",?? yymmdd6.) then
output;
end;
run;
Create macro array:
/*
%helpPackage(macroArray,'%array()')
*/
/* create macro array M from variable yymm */
%array(ds=have, vars=yymm#M, macarray=Y)
/* loop over array M with do_over... */
%put %do_over(M);
/* ...or loop by yourself */
%macro loop();
%do i= &mLbound. %to &mHbound.;
%put %M(&i.); /* <- %M() is a macro array, so you don't have to fight with && */
%end;
%mend;
%loop()
All the best
Bart
P.S. Here are some instructions how to get macroArray package and where the documentation is.
/*
Details about SAS Packages Framework:
- https://github.com/yabwon/SAS_PACKAGES
Tutorial:
- https://github.com/yabwon/HoW-SASPackages
MacroArray documentation:
- https://github.com/SASPAC/macroarray/blob/main/macroarray.md
*/
/* Install SAS Packages Framework and macroaArray package. */
/* Run this only once. */
filename SPFinit url "https://bit.ly/SPFinit";
%include SPFinit;
filename SPFinit clear;
filename packages "<directory/for/packages/>";
%installPackage(SPFinit macroArray)
/* Run this in your SAS Serrion (or add it to autoexec). */
/* Enable framework and load package */
filename packages "<directory/for/packages/>";
%include packages(SPFinit.sas);
%loadPackage(macroArray)
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.