f



sas macro call sas macro call sas macro - possible?

Hi, here is my test data set:

DATA one ;
  INFILE cards ;
  INPUT a b1 b2 b3 b4;
  subjid+1 ;
CARDS ;
1 3 4 7 7
1 6 5 8 8
1 3 4 7 9
1 3 3 6 8
2 1 2 5 10
2 2 3 6 10
2 2 4 5 9
2 2 3 6 11
;
RUN ;

then I tried this code:
%macro pnt(data);
  proc print data=&data.; run;
%mend pnt;

%macro loopSubset(data, subbasedon, funcmacro);
  proc sort data=&data. nodupkey out=sublist(keep=&subbasedon.);
    by &subbasedon.;
  run;

  PROC Sql;
      select count(*) as cnt into : totlist
        from sublist
    quit;
  run;

  *****  loop all subsets *****;
  %do n=1 %to &totlist.;
    data _null_;
      set sublist (firstobs=&n. obs=&n.);
        call symput('subname',trim(left(&subbasedon.)));
    run;
    data sub&subname.; set &data.;
      if &subbasedon. = &subname.;
    run;

    %&funcmacro.;

  %end;
%mend loopSubset;

***** example to call this robust macro *****;
/********************************************
%loopSubset( data=one,
             subbasedon=a,
             funcmacro=pnt(data=sub&subname.)
           );

no problem!

then I try this:
%macro LoopVars(Data2, VarsNotInLoop, funcmacro2);

  /*** keep only vars you want to do loop ***/;
  data loopVars; set &Data2.;
    drop &VarsNotInLoop.;
  run;
  proc contents data=loopVars out=phenovars(keep = name); run;
  PROC Sql;
    select count(*) as cnt into : totphenolist
      from phenoVars;
  quit;

  *****  loop all the required pheno vars  *****;
  %do m=1 %to &totphenolist.;
      data _null_;
        set phenovars (firstobs=&m. obs=&m.);
        call symput('phenoname',trim(left(name)));
      run;

      /* excute functional predefinite macro */
      %&funcmacro2.;
   %end;
%mend LoopVars;

%macro macro2(data, variable);
   proc univariate data = &data. noprint;
    label &variable. = &variable.;
    var &variable.;
    histogram &variable. /normal;
   run;
);
%mend;


%LoopVars(Data2 = one,
          VarsNotInLoop= a subjid,
          funcmacro2= macro2(data=&data2., variable=&phenoname.)
          );

still ok!

But when I call loopsubset which further call loopvars, it doesn't
work.

%loopSubset( data=one,
             subbasedon=a,
             funcmacro=LoopVars(Data2 = sub&subname.,
                                VarsNotInLoop= a subjid,
                                funcmacro2= macro2(data=&data2.,
 
variable=&phenoname.,
                                                    )
                                )

            );


I got:
WARNING: Apparent symbolic reference DATA2 not resolved.
WARNING: Apparent symbolic reference PHENONAME not resolved.

How can I solve the problem???

Thanks,

Shiping

0
2/15/2007 8:32:26 PM
comp.soft-sys.sas 142828 articles. 3 followers. Post Follow

3 Replies
1203 Views

Similar Articles

[PageSpeed] 16

Shiping,

Your first "no problem" has a problem.  Sublist is never defined!

Additionally, your proc sql ends with a run statement (not a problem,
but will cause a comment in the log), and all of your macro calls end
with semi-colons (which, I think, could end up causing problems).

I didn't trace the code beyond those errors, since the other calls
invoke the same macros.

Art
-----------
On Feb 15, 3:32 pm, "sas_9264" <Shiping9...@gmail.com> wrote:
> Hi, here is my test data set:
>
> DATA one ;
>   INFILE cards ;
>   INPUT a b1 b2 b3 b4;
>   subjid+1 ;
> CARDS ;
> 1 3 4 7 7
> 1 6 5 8 8
> 1 3 4 7 9
> 1 3 3 6 8
> 2 1 2 5 10
> 2 2 3 6 10
> 2 2 4 5 9
> 2 2 3 6 11
> ;
> RUN ;
>
> then I tried this code:
> %macro pnt(data);
>   proc print data=&data.; run;
> %mend pnt;
>
> %macro loopSubset(data, subbasedon, funcmacro);
>   proc sort data=&data. nodupkey out=sublist(keep=&subbasedon.);
>     by &subbasedon.;
>   run;
>
>   PROC Sql;
>       select count(*) as cnt into : totlist
>         from sublist
>     quit;
>   run;
>
>   *****  loop all subsets *****;
>   %do n=1 %to &totlist.;
>     data _null_;
>       set sublist (firstobs=&n. obs=&n.);
>         call symput('subname',trim(left(&subbasedon.)));
>     run;
>     data sub&subname.; set &data.;
>       if &subbasedon. = &subname.;
>     run;
>
>     %&funcmacro.;
>
>   %end;
> %mend loopSubset;
>
> ***** example to call this robust macro *****;
> /********************************************
> %loopSubset( data=one,
>              subbasedon=a,
>              funcmacro=pnt(data=sub&subname.)
>            );
>
> no problem!
>
> then I try this:
> %macro LoopVars(Data2, VarsNotInLoop, funcmacro2);
>
>   /*** keep only vars you want to do loop ***/;
>   data loopVars; set &Data2.;
>     drop &VarsNotInLoop.;
>   run;
>   proc contents data=loopVars out=phenovars(keep = name); run;
>   PROC Sql;
>     select count(*) as cnt into : totphenolist
>       from phenoVars;
>   quit;
>
>   *****  loop all the required pheno vars  *****;
>   %do m=1 %to &totphenolist.;
>       data _null_;
>         set phenovars (firstobs=&m. obs=&m.);
>         call symput('phenoname',trim(left(name)));
>       run;
>
>       /* excute functional predefinite macro */
>       %&funcmacro2.;
>    %end;
> %mend LoopVars;
>
> %macro macro2(data, variable);
>    proc univariate data = &data. noprint;
>     label &variable. = &variable.;
>     var &variable.;
>     histogram &variable. /normal;
>    run;
> );
> %mend;
>
> %LoopVars(Data2 = one,
>           VarsNotInLoop= a subjid,
>           funcmacro2= macro2(data=&data2., variable=&phenoname.)
>           );
>
> still ok!
>
> But when I call loopsubset which further call loopvars, it doesn't
> work.
>
> %loopSubset( data=one,
>              subbasedon=a,
>              funcmacro=LoopVars(Data2 = sub&subname.,
>                                 VarsNotInLoop= a subjid,
>                                 funcmacro2= macro2(data=&data2.,
>
> variable=&phenoname.,
>                                                     )
>                                 )
>
>             );
>
> I got:
> WARNING: Apparent symbolic reference DATA2 not resolved.
> WARNING: Apparent symbolic reference PHENONAME not resolved.
>
> How can I solve the problem???
>
> Thanks,
>
> Shiping


0
art297 (4213)
2/16/2007 3:53:45 AM
Hi Shiping,

As far as I can see there is an illegal statement consisting of ); in
your macro Macro2.
Furthermore the macro variables &data2 and &phenoname will not be
resolved in the same macro call as where they are defined. You would
have to defined only the macro name as the macro parameter value of
LoopVars and inside that macro you have to build the other macro call.

The same principle applies to LoopSubset, that actually does not call
LoopVars, because the % character is lacking.

So, it is quite unclear what you would want to do. Tell us more about
what and why, less about how; there may be quite a different or more
simple solution.

Regards - Jim.
--
Jim Groeneveld, Netherlands
Statistician, SAS consultant
home.hccnet.nl/jim.groeneveld

On Feb 15, 9:32 pm, "sas_9264" <Shiping9...@gmail.com> wrote:
> Hi, here is my test data set:
>
> DATA one ;
>   INFILE cards ;
>   INPUT a b1 b2 b3 b4;
>   subjid+1 ;
> CARDS ;
> 1 3 4 7 7
> 1 6 5 8 8
> 1 3 4 7 9
> 1 3 3 6 8
> 2 1 2 5 10
> 2 2 3 6 10
> 2 2 4 5 9
> 2 2 3 6 11
> ;
> RUN ;
>
> then I tried this code:
> %macro pnt(data);
>   proc print data=&data.; run;
> %mend pnt;
>
> %macro loopSubset(data, subbasedon, funcmacro);
>   proc sort data=&data. nodupkey out=sublist(keep=&subbasedon.);
>     by &subbasedon.;
>   run;
>
>   PROC Sql;
>       select count(*) as cnt into : totlist
>         from sublist
>     quit;
>   run;
>
>   *****  loop all subsets *****;
>   %do n=1 %to &totlist.;
>     data _null_;
>       set sublist (firstobs=&n. obs=&n.);
>         call symput('subname',trim(left(&subbasedon.)));
>     run;
>     data sub&subname.; set &data.;
>       if &subbasedon. = &subname.;
>     run;
>
>     %&funcmacro.;
>
>   %end;
> %mend loopSubset;
>
> ***** example to call this robust macro *****;
> /********************************************
> %loopSubset( data=one,
>              subbasedon=a,
>              funcmacro=pnt(data=sub&subname.)
>            );
>
> no problem!
>
> then I try this:
> %macro LoopVars(Data2, VarsNotInLoop, funcmacro2);
>
>   /*** keep only vars you want to do loop ***/;
>   data loopVars; set &Data2.;
>     drop &VarsNotInLoop.;
>   run;
>   proc contents data=loopVars out=phenovars(keep = name); run;
>   PROC Sql;
>     select count(*) as cnt into : totphenolist
>       from phenoVars;
>   quit;
>
>   *****  loop all the required pheno vars  *****;
>   %do m=1 %to &totphenolist.;
>       data _null_;
>         set phenovars (firstobs=&m. obs=&m.);
>         call symput('phenoname',trim(left(name)));
>       run;
>
>       /* excute functional predefinite macro */
>       %&funcmacro2.;
>    %end;
> %mend LoopVars;
>
> %macro macro2(data, variable);
>    proc univariate data = &data. noprint;
>     label &variable. = &variable.;
>     var &variable.;
>     histogram &variable. /normal;
>    run;
> );
> %mend;
>
> %LoopVars(Data2 = one,
>           VarsNotInLoop= a subjid,
>           funcmacro2= macro2(data=&data2., variable=&phenoname.)
>           );
>
> still ok!
>
> But when I call loopsubset which further call loopvars, it doesn't
> work.
>
> %loopSubset( data=one,
>              subbasedon=a,
>              funcmacro=LoopVars(Data2 = sub&subname.,
>                                 VarsNotInLoop= a subjid,
>                                 funcmacro2= macro2(data=&data2.,
>
> variable=&phenoname.,
>                                                     )
>                                 )
>
>             );
>
> I got:
> WARNING: Apparent symbolic reference DATA2 not resolved.
> WARNING: Apparent symbolic reference PHENONAME not resolved.
>
> How can I solve the problem???
>
> Thanks,
>
> Shiping


0
jim4stat (166)
2/16/2007 11:46:29 AM
Hi Jim, Thank you to exam my code.

On Feb 16, 5:46 am, jim4s...@yahoo.co.uk wrote:
> Hi Shiping,
>
> As far as I can see there is an illegal statement consisting of ); in
> your macro Macro2.
Yes, I notice that after I post.

> Furthermore the macro variables &data2 and &phenoname will not be
> resolved in the same macro call as where they are defined. You would
> have to defined only the macro name as the macro parameter value of
> LoopVars and inside that macro you have to build the other macro call.
>
> The same principle applies to LoopSubset, that actually does not call
> LoopVars, because the % character is lacking.
>
> So, it is quite unclear what you would want to do. Tell us more about
> what and why, less about how; there may be quite a different or more
> simple solution.
>
Basically what I want to do is have universal macro that can go over
each subset of data and loop over each var to perform certain
function, for example to do a plot. Once I have it, the only thing I
need to do is just plug in a function macro. It's like cassette
player, if I like different music, then put different tape. That's my
original idea to test this code. I think probably I need change
strategy that not call loopVars within loopSubset, just to make a kind
of two 'do loop' then call functional macro, I think it will be fine.
What do you think? Alternative approach?

Shiping
> Regards - Jim.
> --
> Jim Groeneveld, Netherlands
> Statistician, SAS consultant
> home.hccnet.nl/jim.groeneveld
>
> On Feb 15, 9:32 pm, "sas_9264" <Shiping9...@gmail.com> wrote:
>
> > Hi, here is my test data set:
>
> > DATA one ;
> >   INFILE cards ;
> >   INPUT a b1 b2 b3 b4;
> >   subjid+1 ;
> > CARDS ;
> > 1 3 4 7 7
> > 1 6 5 8 8
> > 1 3 4 7 9
> > 1 3 3 6 8
> > 2 1 2 5 10
> > 2 2 3 6 10
> > 2 2 4 5 9
> > 2 2 3 6 11
> > ;
> > RUN ;
>
> > then I tried this code:
> > %macro pnt(data);
> >   proc print data=&data.; run;
> > %mend pnt;
>
> > %macro loopSubset(data, subbasedon, funcmacro);
> >   proc sort data=&data. nodupkey out=sublist(keep=&subbasedon.);
> >     by &subbasedon.;
> >   run;
>
> >   PROC Sql;
> >       select count(*) as cnt into : totlist
> >         from sublist
> >     quit;
> >   run;
>
> >   *****  loop all subsets *****;
> >   %do n=1 %to &totlist.;
> >     data _null_;
> >       set sublist (firstobs=&n. obs=&n.);
> >         call symput('subname',trim(left(&subbasedon.)));
> >     run;
> >     data sub&subname.; set &data.;
> >       if &subbasedon. = &subname.;
> >     run;
>
> >     %&funcmacro.;
>
> >   %end;
> > %mend loopSubset;
>
> > ***** example to call this robust macro *****;
> > /********************************************
> > %loopSubset( data=one,
> >              subbasedon=a,
> >              funcmacro=pnt(data=sub&subname.)
> >            );
>
> > no problem!
>
> > then I try this:
> > %macro LoopVars(Data2, VarsNotInLoop, funcmacro2);
>
> >   /*** keep only vars you want to do loop ***/;
> >   data loopVars; set &Data2.;
> >     drop &VarsNotInLoop.;
> >   run;
> >   proc contents data=loopVars out=phenovars(keep = name); run;
> >   PROC Sql;
> >     select count(*) as cnt into : totphenolist
> >       from phenoVars;
> >   quit;
>
> >   *****  loop all the required pheno vars  *****;
> >   %do m=1 %to &totphenolist.;
> >       data _null_;
> >         set phenovars (firstobs=&m. obs=&m.);
> >         call symput('phenoname',trim(left(name)));
> >       run;
>
> >       /* excute functional predefinite macro */
> >       %&funcmacro2.;
> >    %end;
> > %mend LoopVars;
>
> > %macro macro2(data, variable);
> >    proc univariate data = &data. noprint;
> >     label &variable. = &variable.;
> >     var &variable.;
> >     histogram &variable. /normal;
> >    run;
> > );
> > %mend;
>
> > %LoopVars(Data2 = one,
> >           VarsNotInLoop= a subjid,
> >           funcmacro2= macro2(data=&data2., variable=&phenoname.)
> >           );
>
> > still ok!
>
> > But when I call loopsubset which further call loopvars, it doesn't
> > work.
>
> > %loopSubset( data=one,
> >              subbasedon=a,
> >              funcmacro=LoopVars(Data2 = sub&subname.,
> >                                 VarsNotInLoop= a subjid,
> >                                 funcmacro2= macro2(data=&data2.,
>
> > variable=&phenoname.,
> >                                                     )
> >                                 )
>
> >             );
>
> > I got:
> > WARNING: Apparent symbolic reference DATA2 not resolved.
> > WARNING: Apparent symbolic reference PHENONAME not resolved.
>
> > How can I solve the problem???
>
> > Thanks,
>
> > Shiping


0
2/16/2007 6:55:30 PM
Reply:

Similar Artilces:

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

SAS ETL studio
Hi, I am trying to develop a datamart on SAS ETL studio which I had otherwise developed using BASE SAS & Macro SAS. However, I am facing some problem in developing the datamart through ETL studio. I have one main macro that reads in the process (basically macro) names that are to be executed and then executes them one after another. The process information is logged in control tables (also on SAS). The information logged is related to start/end date-time of the process, the return code etc. I am not quite sure how I can use ETL studio for achieving the same objective. I can create the p...

Re: SAS ETL studio
Ankit, It's unclear to me what you want to do with the existing macros and the calling them from ETL Studio. But a few things come to mind. 1. ETL Studio jobs which are to be run in production must be deployed. Deployment directories are defined in Management Console and associated with a SAS Batch Server. When a job is deployed, ETL Studio generates a static SAS program in the deployment directory. Until the job is deployed the job only exists in metadata. 2. Macros can be called by User Written Code transformations that you place in the job, in the pre- and post-process steps for the ...

SAS macro and SAS Proc SQL
Any one have some books recommended for learning SAS macros and SAS Proc SQL? Thanks. Hi Mini, For SAS macros, I'd highly recommend "Carpenter's Complete Guide to the Macro Language" by Art Carpenter (http://www.sas.com/apps/pubscat/bookdetails.jsp?catid=1&pc=59224). Hope this helps, Nancy Nancy Brucken brucken@provide.net On Thu, 21 Jul 2005 14:04:53 -0700, Mini <minzheng@COMCAST.NET> wrote: >Any one have some books recommended for learning SAS macros and SAS >Proc SQL? Thanks. ...

SAS Macro to delete a SAS dataset
Here=92s a simple SAS Macro to delete a SAS dataset=85It takes the name of the SAS dataset as the parameter=85 %macro deletedsn(dsname); %if %index(&dsname,'.') eq 0 %then %do; %let lib=3Dwork; %let dsn=3D&dsname; %end; %else %if %index(&dsname,'.') eq 0 %then %do; %let lib=3D%scan(&dsname, 1,'.'); %let dsn=3D%scan(&dsname,1,'.'); %end; proc datasets lib=3D&lib nolist; delete &dsn; quit; %mend deletedsn; %deletedsn(new); %deletedsn(somelib.new); Read more @ http://sastechies.blogspot.com/2009/12/sas-macro...

Need SAS Data Integration Consultant with SAS Data Integration Studio, Base SAS, SAS macros and SQL experience for a 9 months project at Charlotte NC, please send resumes to kamesh@kmmtechnologies.com
Need SAS Data Integration Consultant with SAS Data Integration Studio, Base SAS, SAS macros and SQL experience for a 9 months project at Charlotte NC, please send resumes to kamesh@kmmtechnologies.com ...

Need SAS Data Integration Consultant with SAS Data Integration Studio, Base SAS, SAS macros and SQL experience for a 9 months project at Charlotte NC, please send resumes to kamesh@kmmtechnologies.com
Need SAS Data Integration Consultant with SAS Data Integration Studio, Base SAS, SAS macros and SQL experience for a 9 months project at Charlotte NC, please send resumes to kamesh@kmmtechnologies.com ...

Need SAS Data Integration Consultant with SAS Data Integration Studio, Base SAS, SAS macros and SQL experience for a 9 months project at Charlotte NC, please send resumes to kamesh@kmmtechnologies.com
Need SAS Data Integration Consultant with SAS Data Integration Studio, Base SAS, SAS macros and SQL experience for a 9 months project at Charlotte NC, please send resumes to kamesh@kmmtechnologies.com ...

Storing SAS-programs and -macros in SAS-catalogs
Hi! I am working on a mainframe-platform where SAS-programs and -macros are stored in PDS-datasets. However this way of storing is platform-dependent in the sense, that access from a PC is a little difficult. Storing programs and macros in SAS-catalogs solves this platform-dependent-problem (and a great advantage in this context is that you can easily add a mainframe-SAS-catalog containing macros in your 'sasautos' on a PC), but working with catalogs on a mainframe seems to be a little more awkward than just working with PDS-datasets, because the SAS-editor supports PDS-datasets very...

Re: SAS macro and SAS Proc SQL
Hi Mini, For SAS macros, I'd highly recommend "Carpenter's Complete Guide to the Macro Language" by Art Carpenter (http://www.sas.com/apps/pubscat/bookdetails.jsp?catid=1&pc=59224). Hope this helps, Nancy Nancy Brucken brucken@provide.net On Thu, 21 Jul 2005 14:04:53 -0700, Mini <minzheng@COMCAST.NET> wrote: >Any one have some books recommended for learning SAS macros and SAS >Proc SQL? Thanks. ...

Calling SAS macro From Unix
Have a doubt in calling the SAS macro from Unix /temp/programs/My_command.sas -SYSPARM $1 -log /temp/log/ My_command.#Y#m#d#v.log -print temp/log/My_command.#Y#m#d#v.lst - LOGPARM "rollover=session" when i run this first time i get the log like My_command201209181.log My_command.#Y#m#d#v.lst when i run this Second time i get the log like My_command201209182.log My_command.#Y#m#d#v.lst The #Y#m#d#v is getting resolved for .log but for .lst its not getting resolved . is there any reason for it ???? Thanks in Advance Saranya.S ...

Re: SAS Macro to delete a SAS dataset 196997
> From: SAS Techies > Subject: SAS Macro to delete a SAS dataset > > Here's a simple SAS Macro to delete a SAS dataset...It takes the name of > the SAS dataset as the parameter... > > %macro deletedsn(dsname); > > %if %index(&dsname,'.') eq 0 %then %do; %let lib=work; %let > dsn=&dsname; %end; > %else %if %index(&dsname,'.') eq 0 %then %do; %let lib=%scan(&dsname, > 1,'.'); %let dsn=%scan(&dsname,1,'.'); %end; > > proc datasets lib=&lib nolist; > delete &dsn; > qui...

Re: SAS Macro to delete a SAS dataset 131840
well, this one reminds me another thread recently discussed: http://www.listserv.uga.edu/cgi-bin/wa?A2=ind0911c&L=sas-l&F=&S=&P=4034 On Wed, 2 Dec 2009 08:50:34 -0800, SAS Techies <sastechiesblog@GMAIL.COM> wrote: >Here?s a simple SAS Macro to delete a SAS dataset?It takes the name of >the SAS dataset as the parameter? > >%macro deletedsn(dsname); > >%if %index(&dsname,'.') eq 0 %then %do; %let lib=work; %let >dsn=&dsname; %end; >%else %if %index(&dsname,'.') eq 0 %then %do; %let lib=%scan(&dsnam...

Re: SAS Macro to delete a SAS dataset 351242
Montura wrote: > class delete; > runInterface: method; > delete(symget('sasDatasetName')); > endmethod; > endclass; > > > Works fine if you use plain SCL. > you could also embed a Proc Datasets > > class delete; > runInterface: method; > submit continue; > proc datasets lib=work kill nolist memtype=DATSA; > > quit; > endsubmit; > endmethod; > endclass; > > > Either way, Macro is harder to read, write and understand. Except that I've ne...

SAS Intrnet question, passing macro variables to SAS.
I got a problem when I tried to pass a variable from URL to SAS. Part of the URL are listed below. When I was trying to pass the macro variable "citystate" to SAS, it doesn't work. The annoying part is the "%str". Does anybody has the experience how to pass a variable with "%str(something)" from URL to SAS? How shall I modify the URL below to make it work? Any answers/suggestions are highly appreciated! Thanks, Jia Lan http://www.mysite.com/cgi-bin/broker?_SERVICE=default&_PROGRAM=mysas.sas &libdata=test&citystate=%str('Birmingham, AL',...