插入这行,结果运行不断,只能强行终止。
CALL EXECUTE('%region('|| x ||')');作者: shiyiming 时间: 2008-10-13 13:32 标题: Re: 菜鸟请教 -- 如何循环? #1. I forgot a semicolon after '%region(&office)'. For reference, please read <!-- m --><a class="postlink" href="http://support.sas.com/onlinedoc/913/getDoc/zh/lrdict.hlp/a000127856.htm">http://support.sas.com/onlinedoc/913/ge ... 127856.htm</a><!-- m --> or <!-- m --><a class="postlink" href="http://www2.sas.com/proceedings/forum2007/050-2007.pdf">http://www2.sas.com/proceedings/forum2007/050-2007.pdf</a><!-- m -->
#2. your DATA Step needs a couple of minor changes to avoid endless loop.
/*
Data _NULL_;
Do until(EndoFile);
set in.temp END=EndOFile;
x = office;
CALL EXECUTE('%region(' || x || ')');
End;
run;
*/
#3. the Little SAS book are not written from a programmer's point of view, and its intended audience are PhD's, researchers, but unfortunately does no good and are sometimes even misleading to bottom-level computer slaves.作者: shiyiming 时间: 2008-10-13 16:28 标题: Re: 菜鸟请教 -- 如何循环? 一个相似的例子。
[code:3l441zzd]data temp;
input office ;
num=ceil(ranuni(0)*8);
cards;
1
2
3
4
5
6
7
8
;
run;
options symbolgen mprint mlogic;
%macro just(x);
data _null_;
if &x>=5 then put 'big';
else put 'small';
run;
%mend;
Data aa;
set temp;
if _n_ >=1 then call execute("%just("||office||")");
run;
[/code:3l441zzd]作者: shiyiming 时间: 2008-10-14 10:11 标题: Re: 菜鸟请教 -- 如何循环? 谢谢 horse1,
我希望的效果正是,
Office Size
1 small
2 small
3 small
4 small
5 big
6 big
7 big
8 big
为测试方便,我将您的 Macro 改为
%macro just(x);
* data _null_;
data test;
set temp(keep=office);
if &x>=5 then size= 'Big';
else size='Small';
run;
%mend;
data aa;
set temp;
if _n_ >=1 then call execute("%just("||office||")");
run;作者: shiyiming 时间: 2008-10-15 10:43 标题: Re: 菜鸟请教 -- 如何循环? 谢谢两位老师指点,我最后对付成这样
%Macro Region(Office);
%Let Office=&Office; /*I found I have to add this line here. */
Data in.One_Office;
set in.Products;
If _Office='&Office' ; /* I have to put quotes here.*/
Run;
Proc Freq data=in.One_Office Order = Freq NoPrint;
Table Product / out = in.NewFreq ;
Run;
Proc Sort Data=in.One_Office Out=in.Unique&Office Nodupkey;
By client_no;
Run;
%Mend ;
Data _NULL_;
Do until(EndoFile); /* Not sure if we can us do while loop here. */
set in.temp END=EndOFile ;
x = Office;
call Execute("%region("|| x ||")");
End;
Run;作者: shiyiming 时间: 2008-10-15 21:03 标题: Re: 菜鸟请教 -- 如何循环? No "老师", I'm just nobody.
(1) the %let statement is unnecessary. As a parameter, &office should exist in the local symbol table already. The %let statement only trims the leading and trailing blanks. This can be done with CALL EXECUTE("%regin(" || left(office) || ")");
(2) there's no need of subsetting dataset 'in.products' unless the subset is used many times. A WHERE statement or WHERE= option in PROC FREQ / PROC SORT is what people usually do.
(3) use double quotes --- "&office"
(4) either do...while... or do...until... will work as long as your dataset is non-empty (slightly more work to cover the case of empty).
/*
Data _NULL_;
EndoFile=0;
Do while(NOT EndoFile);
set in.temp END=EndOFile ;
call Execute("%region("|| office ||")");
End;
Run;
*/作者: shiyiming 时间: 2008-10-16 06:01 标题: Re: 菜鸟请教 -- 如何循环? You're the man, urchin,
Thanks a lot for your detailed explanation.
However, it does not work if I
1. remove the %let .... line.
2. change the double quotes to single quotes,
it became: [color=#4040FF:kpszkujj]If _office="|| office ||" [/color:kpszkujj]; instead of [color=#4040FF:kpszkujj] if _office='Z';[/color:kpszkujj]
How can I add a where statement in Proc Freq or Proc Sort.... so that I can save the trouble of creating the in.One_office dataset?
Your "Do while" works great ! That's really what I want to know.
In SAS, do we have a similar loop like Scan .......End ?
Thanks again.作者: shiyiming 时间: 2008-10-16 23:31 标题: Re: 菜鸟请教 -- 如何循环? (1) my bad, it seems that the CALL EXECUTE routine doesn't like double quotes, so I should have used [u:1ez4pmu5]CALL EXECUTE('%region(' || compress(office) || ')');[/u:1ez4pmu5] instead.
(2) never heard of scan...end... maybe just because I don't know much.作者: shiyiming 时间: 2008-10-17 11:41 标题: Re: 菜鸟请教 -- 如何循环? For whatever reason, I still have to keep , even I say .... compress(office)
%Let ........
and single quotes.
Thanks.作者: shiyiming 时间: 2008-10-17 12:37 标题: Re: 菜鸟请教 -- 如何循环? I don't know. it's simply beyond me. The Indians I know would call this scenario "artificial intelligence", "machine learning", or "soft computing", because same piece of code are yielding different results!作者: shiyiming 时间: 2008-10-17 20:37 标题: Re: 菜鸟请教 -- 如何循环? Got you, urchin, U really know a lot.