f



Using SAS macro to generate SAS macros

I am trying to write a macro to create a whole bunch of macros. Is this possible?   The error I get is "Invalid macro name &".  SAS 9.2 on Windows.  The code follows.  I think that I somehow need to stop the %macro from resolving before the &macname does.
Thanks much,
gene

%macro createmac(macname,theview,sex,startage,endage) ;
	%macro &macname. ;
	proc sql;
	create view &macname._VW as
	select distinct e.*
	from wviews.&theview as e inner join &_vdw_demographic as d
	on e.mrn = d.mrn
	where (("01jan2011"d - birth_Date) / 364.25 ) between &startage and &endage and
	      gender = "&sex."
	;
%let _vdw_enroll=&macname._VW;
  %let _SiteAbbr = &macname;
title3 "Enrollment view is &_vdw_enroll ";
%mend &macname;

%mend createmac ;      

%createmac(VDW_GpdWestWomen50to75,enroll_west_Gpd_vw,F,50,70);
%createmac(VDW_GpdWestMen50to75  ,enroll_west_Gpd_vw,M,50,70);
%createmac(VDW_GpdEastWomen50to75,enroll_East_Gpd_vw,F,50,70);
%createmac(VDW_GpdEastMen50to75  ,enroll_East_Gpd_vw,M,50,70);
%createmac(VDW_NetWestWomen50to75,enroll_west_Net_vw,F,50,70);
%createmac(VDW_NetWestMen50to75  ,enroll_west_Net_vw,M,50,70);
%createmac(VDW_NetEastWomen50to75,enroll_East_Net_vw,F,50,70);
%createmac(VDW_NetEastMen50to75  ,enroll_East_Net_vw,M,50,70);
0
genehart (6)
8/1/2012 11:11:04 PM
comp.soft-sys.sas 142828 articles. 3 followers. Post Follow

2 Replies
1065 Views

Similar Articles

[PageSpeed] 7

On Wednesday, August 1, 2012 4:11:04 PM UTC-7, Gene  Hart wrote:
> I am trying to write a macro to create a whole bunch of macros. Is this p=
ossible?   The error I get is "Invalid macro name &".  SAS 9.2 on Windows. =
 The code follows.  I think that I somehow need to stop the %macro from res=
olving before the &macname does.
>=20
> Thanks much,
>=20
> gene
>=20
>=20
>=20
> %macro createmac(macname,theview,sex,startage,endage) ;
>=20
> 	%macro &macname. ;
>=20
> 	proc sql;
>=20
> 	create view &macname._VW as
>=20
> 	select distinct e.*
>=20
> 	from wviews.&theview as e inner join &_vdw_demographic as d
>=20
> 	on e.mrn =3D d.mrn
>=20
> 	where (("01jan2011"d - birth_Date) / 364.25 ) between &startage and &end=
age and
>=20
> 	      gender =3D "&sex."
>=20
> 	;
>=20
> %let _vdw_enroll=3D&macname._VW;
>=20
>   %let _SiteAbbr =3D &macname;
>=20
> title3 "Enrollment view is &_vdw_enroll ";
>=20
> %mend &macname;
>=20
>=20
>=20
> %mend createmac ;     =20
>=20
>=20
>=20
> %createmac(VDW_GpdWestWomen50to75,enroll_west_Gpd_vw,F,50,70);
>=20
> %createmac(VDW_GpdWestMen50to75  ,enroll_west_Gpd_vw,M,50,70);
>=20
> %createmac(VDW_GpdEastWomen50to75,enroll_East_Gpd_vw,F,50,70);
>=20
> %createmac(VDW_GpdEastMen50to75  ,enroll_East_Gpd_vw,M,50,70);
>=20
> %createmac(VDW_NetWestWomen50to75,enroll_west_Net_vw,F,50,70);
>=20
> %createmac(VDW_NetWestMen50to75  ,enroll_west_Net_vw,M,50,70);
>=20
> %createmac(VDW_NetEastWomen50to75,enroll_East_Net_vw,F,50,70);
>=20
> %createmac(VDW_NetEastMen50to75  ,enroll_East_Net_vw,M,50,70);

This solution is ugly, but it works.  Write the macro code to a temporary f=
ile, then %include it.

filename ifile temp;
%macro createmac(macname,theview,sex,startage,endage) ;
data _null_;
file ifile mod;
put '%macro ' "&macname. ;                                                 =
              ";=20
put "	proc sql;                                                            =
              ";
put "	create view &macname._VW as                                          =
              ";
put "	select distinct e.*                                                  =
              ";
put "	from wviews.&theview as e inner join &_vdw_demographic as d          =
              ";
put "	on e.mrn =3D d.mrn                                                   =
                ";
put '	where (("01jan2011"d - birth_Date) / 364.25 ) ' "between &startage an=
d &endage and ";
put "	      gender =3D " "'" "&sex." "'"                                   =
                 ;
put "	;                                                                    =
              ";
put '%let _vdw_enroll=3D' "&macname._VW;                                   =
                ";
put '%let _SiteAbbr=3D' "&macname;                                         =
                 ";
put '%let _SiteName=3D' "&macname;                                         =
                 ";
put 'title3 "Enrollment view is &_vdw_enroll ";                            =
               ';
put "%mend &macname;                                                       =
               ";
%mend createmac;                                                           =
               =20

%createmac(VDW_GpdWestWomen50to75,enroll_west_Gpd_vw,F,50,70);
%createmac(VDW_GpdWestMen50to75  ,enroll_west_Gpd_vw,M,50,70);
%createmac(VDW_GpdEastWomen50to75,enroll_East_Gpd_vw,F,50,70);
%createmac(VDW_GpdEastMen50to75  ,enroll_East_Gpd_vw,M,50,70);
%createmac(VDW_NetWestWomen50to75,enroll_west_Net_vw,F,50,70);
%createmac(VDW_NetWestMen50to75  ,enroll_west_Net_vw,M,50,70);
%createmac(VDW_NetEastWomen50to75,enroll_East_Net_vw,F,50,70);
%createmac(VDW_NetEastMen50to75  ,enroll_East_Net_vw,M,50,70);
%createmac(VDW_GpdWestWomen50to75,enroll_west_Gpd_vw,F,50,70);

%include ifile;
0
genehart (6)
8/2/2012 5:45:07 PM
On Aug 3, 1:45=A0am, Gene  Hart <geneh...@gmail.com> wrote:
> On Wednesday, August 1, 2012 4:11:04 PM UTC-7, Gene =A0Hart wrote:
> > I am trying to write a macro to create a whole bunch of macros. Is this=
 possible? =A0 The error I get is "Invalid macro name &". =A0SAS 9.2 on Win=
dows. =A0The code follows. =A0I think that I somehow need to stop the %macr=
o from resolving before the &macname does.
>
> > Thanks much,
>
> > gene
>
> > %macro createmac(macname,theview,sex,startage,endage) ;
>
> > =A0 =A0%macro &macname. ;
>
> > =A0 =A0proc sql;
>
> > =A0 =A0create view &macname._VW as
>
> > =A0 =A0select distinct e.*
>
> > =A0 =A0from wviews.&theview as e inner join &_vdw_demographic as d
>
> > =A0 =A0on e.mrn =3D d.mrn
>
> > =A0 =A0where (("01jan2011"d - birth_Date) / 364.25 ) between &startage =
and &endage and
>
> > =A0 =A0 =A0 =A0 =A0gender =3D "&sex."
>
> > =A0 =A0;
>
> > %let _vdw_enroll=3D&macname._VW;
>
> > =A0 %let _SiteAbbr =3D &macname;
>
> > title3 "Enrollment view is &_vdw_enroll ";
>
> > %mend &macname;
>
> > %mend createmac ;
>
> > %createmac(VDW_GpdWestWomen50to75,enroll_west_Gpd_vw,F,50,70);
>
> > %createmac(VDW_GpdWestMen50to75 =A0,enroll_west_Gpd_vw,M,50,70);
>
> > %createmac(VDW_GpdEastWomen50to75,enroll_East_Gpd_vw,F,50,70);
>
> > %createmac(VDW_GpdEastMen50to75 =A0,enroll_East_Gpd_vw,M,50,70);
>
> > %createmac(VDW_NetWestWomen50to75,enroll_west_Net_vw,F,50,70);
>
> > %createmac(VDW_NetWestMen50to75 =A0,enroll_west_Net_vw,M,50,70);
>
> > %createmac(VDW_NetEastWomen50to75,enroll_East_Net_vw,F,50,70);
>
> > %createmac(VDW_NetEastMen50to75 =A0,enroll_East_Net_vw,M,50,70);
>
> This solution is ugly, but it works. =A0Write the macro code to a tempora=
ry file, then %include it.
>
> filename ifile temp;
> %macro createmac(macname,theview,sex,startage,endage) ;
> data _null_;
> file ifile mod;
> put '%macro ' "&macname. ; =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=
 =A0 ";
> put " =A0 =A0 =A0proc sql; =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=
 =A0 =A0 =A0 =A0 =A0 =A0 =A0";
> put " =A0 =A0 =A0create view &macname._VW as =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=
 =A0 =A0";
> put " =A0 =A0 =A0select distinct e.* =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=
 =A0 =A0 =A0 =A0";
> put " =A0 =A0 =A0from wviews.&theview as e inner join &_vdw_demographic a=
s d =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0";
> put " =A0 =A0 =A0on e.mrn =3D d.mrn =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=
 =A0 =A0 =A0 =A0 =A0 ";
> put ' =A0 where (("01jan2011"d - birth_Date) / 364.25 ) ' "between &start=
age and &endage and ";
> put " =A0 =A0 =A0 =A0 =A0 =A0gender =3D " "'" "&sex." "'" =A0 =A0 =A0 =A0=
 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0;
> put " =A0 =A0 =A0; =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=
 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0";
> put '%let _vdw_enroll=3D' "&macname._VW; =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 ";
> put '%let _SiteAbbr=3D' "&macname; =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=
 =A0";
> put '%let _SiteName=3D' "&macname; =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=
 =A0";
> put 'title3 "Enrollment view is &_vdw_enroll "; =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 ';
> put "%mend &macname; =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=
 =A0 =A0 =A0";
> %mend createmac;
>
> %createmac(VDW_GpdWestWomen50to75,enroll_west_Gpd_vw,F,50,70);
> %createmac(VDW_GpdWestMen50to75 =A0,enroll_west_Gpd_vw,M,50,70);
> %createmac(VDW_GpdEastWomen50to75,enroll_East_Gpd_vw,F,50,70);
> %createmac(VDW_GpdEastMen50to75 =A0,enroll_East_Gpd_vw,M,50,70);
> %createmac(VDW_NetWestWomen50to75,enroll_west_Net_vw,F,50,70);
> %createmac(VDW_NetWestMen50to75 =A0,enroll_west_Net_vw,M,50,70);
> %createmac(VDW_NetEastWomen50to75,enroll_East_Net_vw,F,50,70);
> %createmac(VDW_NetEastMen50to75 =A0,enroll_East_Net_vw,M,50,70);
> %createmac(VDW_GpdWestWomen50to75,enroll_west_Gpd_vw,F,50,70);
>
> %include ifile;- Hide quoted text -
>
> - Show quoted text -

%macro createmac(macname,theview,sex,startage,endage) ;
        proc sql;
        create view &macname._VW as
        select distinct e.*
        from wviews.&theview as e inner join &_vdw_demographic as d
        on e.mrn =3D d.mrn
        where (("01jan2011"d - birth_Date) / 364.25 ) between
&startage and &endage and
              gender =3D "&sex."
        ;
  %let _vdw_enroll=3D&macname._VW;
  %let _SiteAbbr =3D &macname;
  title3 "Enrollment view is &_vdw_enroll ";
%mend;

data mvalue;
  length macname $ 100 theview $ 100 sex $ 2 ;
  input macname theview sex startage endage;
datalines;
VDW_GpdWestWomen50to75 enroll_west_Gpd_vw F 50 70
VDW_GpdWestMen50to75   enroll_west_Gpd_vw M 50 70
VDW_GpdEastWomen50to75 enroll_East_Gpd_vw F 50 70
VDW_GpdEastMen50to75   enroll_East_Gpd_vw M 50 70
VDW_NetWestWomen50to75 enroll_west_Net_vw F 50 70
VDW_NetWestMen50to75   enroll_west_Net_vw M 50 70
VDW_NetEastWomen50to75 enroll_East_Net_vw F 50 70
VDW_NetEastMen50to75   enroll_East_Net_vw M 50 70
;

data _null_ ;
  set mvalue;
  call execute("%createmac(" ||macname || ", " || theview || ", " ||
sex || ", " || startage || ", " || endage || ")");
run;
0
8/3/2012 1:24:38 AM
Reply: