LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous (more recent) messageNext (less recent) messagePrevious (more recent) in topicNext (less recent) in topicPrevious (more recent) by same authorNext (less recent) by same authorPrevious page (January 2009, week 4)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:   Fri, 23 Jan 2009 14:04:25 -0600
Reply-To:   Joe Matise <snoopy369@GMAIL.COM>
Sender:   "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:   Joe Matise <snoopy369@GMAIL.COM>
Subject:   Re: Loop through values, run macro for each value, create rtf files
Comments:   To: webonomic <webonomic@gmail.com>
In-Reply-To:   <b7a7fa630901231203p4acb49ccw1cf5c87c70de4cf@mail.gmail.com>
Content-Type:   text/plain; charset=ISO-8859-1

One correction -

proc sql; select distinct cats('%run_report(',firstname, > > ');') into :macrocall separated by ' ' from meansset; > quit;

should be

proc sql; select distinct cats('%run_report(',firstname, > > ');') into :macrocall separated by ' ' from names;; > quit;

I had renamed it in my SAS copy :)

-Joe

On Fri, Jan 23, 2009 at 2:03 PM, Joe Matise <snoopy369@gmail.com> wrote:

> First off, run your proc means this way: > > %MACRO run_report(firstname_var); > ods rtf file=%outputfolder(filename="&firstname_var..rtf"); > *you need two . here because macro name goes from & to . inclusive if . is > present, the second . is the actual . for the filename; > proc means data=names; > where compress(firstname)= "&firstname_var"; *by is > not appropriate, as it takes a variable, not a variable value, as argument; > var books; > quit; > ods rtf close; > %MEND run_report; > > I'd also change %outputfolder to instead be a macro variable %let > outputfolder=c:\; because there's no reason you need a macro for that. > > Now you just need to do this: > > > proc sql; > select distinct cats('%run_report(',firstname,');') into :macrocall > separated by ' ' from meansset; > *you do not need namelist unless there is something else going on, distinct > will select each name once; > quit; > > > to generate your macro calls, and then > &macrocall > to actually call them (to do the looping). > > Final program: > > > > data names; > input firstname $ books; > cards; > Alice 2 > Alice 4 > Alice 5 > Jim 6 > Jim 3 > Jim 2 > Jim 2 > Jim 4 > Jim 5 > Peter 1 > Peter 6 > Peter 4 > Peter 3 > Peter 7 > ; > run; > > %let outputfolder=C:\; > > > %MACRO run_report(firstname_var); > ods rtf file="&outputfolder.&firstname_var..rtf"; > *you need two . here because macro name goes from & to . inclusive if . is > present, the second . is the actual . for the filename; > proc means data=names; > where compress(firstname)= "&firstname_var"; *by is > not appropriate, as it takes a variable, not a variable value, as argument; > quit; > ods rtf close; > %MEND run_report; > > proc sql; > select distinct cats('%run_report(',firstname,');') into :macrocall > separated by ' ' from meansset; > quit; > > &macrocall; > > Hope this helps, > > Joe > > > On Fri, Jan 23, 2009 at 1:13 PM, webonomic <webonomic@gmail.com> wrote: > >> I've simplified my problem to make it easier. >> >> I have "name_list" dataset which was created from "names" dataset. I >> want to loop through the values of name_list (i.e. Alice, Jim, and >> Andy). For each name, I want to run a PROC MEANS against the "names" >> dataset and spit out an RTF file with the appropriate name. I should >> end up with 3 different files. >> >> This is what I have so far: >> >> data name_list; >> input firstname $; >> cards; >> Alice >> Jim >> Peter >> ; >> run; >> >> data names; >> input firstname $ books; >> cards; >> Alice 2 >> Alice 4 >> Alice 5 >> Jim 6 >> Jim 3 >> Jim 2 >> Jim 2 >> Jim 4 >> Jim 5 >> Peter 1 >> Peter 6 >> Peter 4 >> Peter 3 >> Peter 7 >> ; >> run; >> >> /*outputfolder macro comes form my macro library*/ >> %MACRO outputfolder(filename=); >> "C:\&filename"; >> %MEND outputfolder; >> %global outputfolder; >> >> /*will I get the right filename? or do I need to go &&name_var ?*/ >> %MACRO run_report(firstname_var); >> ods rtf file=%outputfolder(filename="&name_var.rtf"); >> proc means data=names; >> by &firstname_var; >> quit; >> ods rtf close; >> %MEND run_report; >> >> >> /* code to loop through each obs of NAMES_LIST and run RUN_REPORT >> macro */ >> loop through NAMES values; >> for each run %run_report(firstname); >> end loop; >> > >


Back to: Top of message | Previous page | Main SAS-L page