|
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
> ¯ocall
> 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;
>
> ¯ocall;
>
> 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;
>>
>
>
|