楼主: RobertBarro
9281 10

问一个在数据步中生成宏的问题 [推广有奖]

  • 0关注
  • 0粉丝

初中生

71%

还不是VIP/贵宾

-

威望
0
论坛币
0 个
通用积分
0.1500
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
103 点
帖子
10
精华
0
在线时间
18 小时
注册时间
2008-8-3
最后登录
2025-6-19

楼主
RobertBarro 发表于 2010-3-14 21:41:34 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
小弟最近用宏比较多,但也经常被它烦到了在数据步要生成一个期间(span)的变量
data........;
call symput('span', min(intck('month',startdate,enddate), &recspan, intck('month',startdate,TA500261050)));
要在后面宏do循环中引用这个宏变量,
%put span=&span;
%do i=0 %to &span.;
结果是写得有问题,
以下是sas给出的解释:
WARNING: 没有解析符号引用 SPAN。
ERROR: 在需要数值操作数的 %EVAL 函数或 %IF 条件中发现字符操作数。条件是: &span.
ERROR: %TO 值(%DO I 循环中)无效。
ERROR: 宏 PIL_LGD 将终止执行。
可是我实在是想不出来哪里有问题啊,望大牛赐教一下。多谢!!!
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:warning symput Error month Start 数据

回帖推荐

bobguy 发表于4楼  查看完整内容

In a usually case, a macro variable defined in a data step cannot be referred in that step (too soon). A data step needs a clear boundary .-- which begins with data ...; and end with run; symputx代替symput: yes. symputx will do data data type convertion and trim trailing blanks autometically. I am not clear for your problem. You had better to define your problem as, 1) what you have, 2 ...

bobguy 发表于2楼  查看完整内容

1) Check to see if macro variable span is defined and is in the way you need. time_check=min(intck('month',startdate,enddate), &recspan, intck('month',startdate,TA500261050)); put time_check=; If the value is missing or has decimal, it will cause the proble. 2) USE symputx that is better. data........; call symput('span', min(intck('month',startdate,enddate), &recspan, intck( ...

本帖被以下文库推荐

沙发
bobguy 发表于 2010-3-14 22:23:59
RobertBarro 发表于 2010-3-14 21:41
小弟最近用宏比较多,但也经常被它烦到了在数据步要生成一个期间(span)的变量
data........;
call symput('span', min(intck('month',startdate,enddate), &recspan, intck('month',startdate,TA500261050)));
要在后面宏do循环中引用这个宏变量,
%put span=&span;
%do i=0 %to &span.;
结果是写得有问题,
以下是sas给出的解释:
WARNING: 没有解析符号引用 SPAN。
ERROR: 在需要数值操作数的 %EVAL 函数或 %IF 条件中发现字符操作数。条件是: &span.
ERROR: %TO 值(%DO I 循环中)无效。
ERROR: 宏 PIL_LGD 将终止执行。
可是我实在是想不出来哪里有问题啊,望大牛赐教一下。多谢!!!
1) Check to see if macro variable span is defined and is in the way you need.

time_check=min(intck('month',startdate,enddate), &recspan, intck('month',startdate,TA500261050));
put time_check=;

If the value is missing or has decimal, it will cause the proble.

2) USE symputx that is better.


data........;
call symput('span', min(intck('month',startdate,enddate), &recspan, intck('month',startdate,TA500261050)));
已有 1 人评分经验 论坛币 收起 理由
bakoll + 3 + 3 精彩帖子

总评分: 经验 + 3  论坛币 + 3   查看全部评分

藤椅
RobertBarro 发表于 2010-3-14 23:12:56
大牛,你是说在最后用symputx代替symput么?

另外,我在下边也遇到了同样的问题:
data by.callgd_&obyy._&obmm. ;
set by.callgd_&obyy._&obmm.;
%do i=0 %to &span.;
call symput('aa',intnx('month',startdate,&i.));
call symput('bb',intnx('month',startdate,%eval(&i.-1)));       
%let yy=%sysfunc(year(&aa.));
%let mm=%sysfunc(month(&aa.));
%let by=%sysfunc(year(&bb.));
%let bm=%sysfunc(month(&bb.));

同样是不能引用&aa,&bb的值;似乎是因为call ymput刚刚才用,不能马上引用宏的值? 可是我要用循环不断更新宏变量啊,然后再做函数运算,根本不能分两步来写。。。。。save me~~~help~~~

板凳
bobguy 发表于 2010-3-14 23:37:49
RobertBarro 发表于 2010-3-14 23:12
大牛,你是说在最后用symputx代替symput么?

另外,我在下边也遇到了同样的问题:
data by.callgd_&obyy._&obmm. ;
set by.callgd_&obyy._&obmm.;
%do i=0 %to &span.;
call symput('aa',intnx('month',startdate,&i.));
call symput('bb',intnx('month',startdate,%eval(&i.-1)));        
%let yy=%sysfunc(year(&aa.));
%let mm=%sysfunc(month(&aa.));
%let by=%sysfunc(year(&bb.));
%let bm=%sysfunc(month(&bb.));

同样是不能引用&aa,&bb的值;似乎是因为call ymput刚刚才用,不能马上引用宏的值? 可是我要用循环不断更新宏变量啊,然后再做函数运算,根本不能分两步来写。。。。。save me~~~help~~~
In a usually case, a macro variable defined in a data step cannot be referred in that step (too soon). A data step needs a clear boundary .-- which begins with data ...; and end with run;

symputx代替symput: yes. symputx will do data data type convertion and trim trailing blanks autometically.

I am not clear for your problem. You had better to define your problem as,
1) what you have,
2) what you want,
3)what is the problem.
已有 1 人评分经验 论坛币 收起 理由
bakoll + 3 + 3 精彩帖子

总评分: 经验 + 3  论坛币 + 3   查看全部评分

报纸
RobertBarro 发表于 2010-3-15 00:14:25
谢谢大虾。。。
1)我的数据结构:id variables, startdate, enddate, 一系列计算变量形如x200412-x200712, y200412-y200712 etc. 基本样是横表:one observation with lots of variables at different time points.
2) 我需要根据输入的一个宏变量 &prespan,与intck('month',startdate,enddate)做比较,取最小值记为&span。然后取所有startdate时点的x,y 等算出一个z@startdate, 再移动到x@startdate+1,y@startdate+1算出z@startdate+1, 一直到z@startdate+&span为止。
3)因为我的变量名也希望用startdate,startdate+1....startdate+&span的形式结尾,所以我用了宏do循环,用intnx计算出与startdate距离i个月的日期,然后取year and month做变量名后缀,因为是在一个循环里,所以call symput与下面对宏变量的引用不能分开。我要是用%let  aa=%sysfunc(intnx('month',startdate,&i.))来定义第一个宏变量,它又说startdate不是个数字(而实际上是一个日期了)。

我希望表示清楚了。调这个程序一天了,都快绝望了。。。。
多谢多谢

地板
MyLoveSas 发表于 2010-3-15 13:00:56
很久没学sas了,忘得差不多了。印象中call symput生成的宏变量只有在该数据步结束后才能调用,是不是你调用的时间不对?

7
soporaeternus 发表于 2010-3-15 13:04:41
解析和执行......
Let them be hard, but never unjust

8
gaohualong 发表于 2010-3-15 23:08:43
%put span=&span;

上面这一句之前加上%let span=%symget('span');

%symget()函数可以在同一data步中使用由call %symput()函数创建的宏变量

9
jingju11 发表于 2010-3-16 01:17:31
8# gaohualong

如果在同一个step里生成和使用,很可能你可以避免用macro变量。

10
bobguy 发表于 2010-3-16 09:14:31
RobertBarro 发表于 2010-3-15 00:14
谢谢大虾。。。
1)我的数据结构:id variables, startdate, enddate, 一系列计算变量形如x200412-x200712, y200412-y200712 etc. 基本样是横表:one observation with lots of variables at different time points.
2) 我需要根据输入的一个宏变量 &prespan,与intck('month',startdate,enddate)做比较,取最小值记为&span。然后取所有startdate时点的x,y 等算出一个z@startdate, 再移动到x@startdate+1,y@startdate+1算出z@startdate+1, 一直到z@startdate+&span为止。
3)因为我的变量名也希望用startdate,startdate+1....startdate+&span的形式结尾,所以我用了宏do循环,用intnx计算出与startdate距离i个月的日期,然后取year and month做变量名后缀,因为是在一个循环里,所以call symput与下面对宏变量的引用不能分开。我要是用%let  aa=%sysfunc(intnx('month',startdate,&i.))来定义第一个宏变量,它又说startdate不是个数字(而实际上是一个日期了)。

我希望表示清楚了。调这个程序一天了,都快绝望了。。。。
多谢多谢
I am not sure what exactly you want. The following example may help.

196  data test;
197     array xdt(10)  ;
198      do s='23jun2009'd  to  '3apr2010'd by 30;
199         i+1;
200          xdt(i)=s;
201      end;
202      drop i s;
203  run;

NOTE: The data set WORK.TEST has 1 observations and 10 variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds


204
205  proc print;
206  format _all_ yymmdd10.;
207  run;

NOTE: There were 1 observations read from the data set WORK.TEST.
NOTE: PROCEDURE PRINT used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


208
209  %let span=5;
210  data _null_;
211  set test;
212  array xdt(*) xdt:;
213  do j=1 to dim (xdt);
214  do i=0 to &span;
215     call symputx(catt(vname(xdt(j)),'_',i) ,intnx('month',xdt1,i));
216  end;
217  end;
218  call symputx('n',  dim(xdt));
219  stop;
220  run;

NOTE: There were 1 observations read from the data set WORK.TEST.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds


221
222  %macro showit(dummy);
223      %do i = 1 %to &n;
224          %do j=1 %to &span;
225              %put  >>>>xdt&i._&j  %sysfunc(putn(&&xdt&i._&j, yymmdd10.))
225! %sysfunc(year(&&xdt&i._&j)) %sysfunc(month(&&xdt&i._&j))<<<<;
226           %end;
227      %end;
228  %mend;
229
230  %showit(dummy)
>>>>xdt1_1  2009-07-01 2009 7<<<<
>>>>xdt1_2  2009-08-01 2009 8<<<<
>>>>xdt1_3  2009-09-01 2009 9<<<<
>>>>xdt1_4  2009-10-01 2009 10<<<<
>>>>xdt1_5  2009-11-01 2009 11<<<<
>>>>xdt2_1  2009-07-01 2009 7<<<<
>>>>xdt2_2  2009-08-01 2009 8<<<<
>>>>xdt2_3  2009-09-01 2009 9<<<<
>>>>xdt2_4  2009-10-01 2009 10<<<<
>>>>xdt2_5  2009-11-01 2009 11<<<<
>>>>xdt3_1  2009-07-01 2009 7<<<<
>>>>xdt3_2  2009-08-01 2009 8<<<<
>>>>xdt3_3  2009-09-01 2009 9<<<<
>>>>xdt3_4  2009-10-01 2009 10<<<<
>>>>xdt3_5  2009-11-01 2009 11<<<<
>>>>xdt4_1  2009-07-01 2009 7<<<<
>>>>xdt4_2  2009-08-01 2009 8<<<<
>>>>xdt4_3  2009-09-01 2009 9<<<<
>>>>xdt4_4  2009-10-01 2009 10<<<<
>>>>xdt4_5  2009-11-01 2009 11<<<<
>>>>xdt5_1  2009-07-01 2009 7<<<<
>>>>xdt5_2  2009-08-01 2009 8<<<<
>>>>xdt5_3  2009-09-01 2009 9<<<<
>>>>xdt5_4  2009-10-01 2009 10<<<<
>>>>xdt5_5  2009-11-01 2009 11<<<<
>>>>xdt6_1  2009-07-01 2009 7<<<<
>>>>xdt6_2  2009-08-01 2009 8<<<<
>>>>xdt6_3  2009-09-01 2009 9<<<<
>>>>xdt6_4  2009-10-01 2009 10<<<<
>>>>xdt6_5  2009-11-01 2009 11<<<<
>>>>xdt7_1  2009-07-01 2009 7<<<<
>>>>xdt7_2  2009-08-01 2009 8<<<<
>>>>xdt7_3  2009-09-01 2009 9<<<<
>>>>xdt7_4  2009-10-01 2009 10<<<<
>>>>xdt7_5  2009-11-01 2009 11<<<<
>>>>xdt8_1  2009-07-01 2009 7<<<<
>>>>xdt8_2  2009-08-01 2009 8<<<<
>>>>xdt8_3  2009-09-01 2009 9<<<<
>>>>xdt8_4  2009-10-01 2009 10<<<<
>>>>xdt8_5  2009-11-01 2009 11<<<<
>>>>xdt9_1  2009-07-01 2009 7<<<<
>>>>xdt9_2  2009-08-01 2009 8<<<<
>>>>xdt9_3  2009-09-01 2009 9<<<<
>>>>xdt9_4  2009-10-01 2009 10<<<<
>>>>xdt9_5  2009-11-01 2009 11<<<<
>>>>xdt10_1  2009-07-01 2009 7<<<<
>>>>xdt10_2  2009-08-01 2009 8<<<<
>>>>xdt10_3  2009-09-01 2009 9<<<<
>>>>xdt10_4  2009-10-01 2009 10<<<<
>>>>xdt10_5  2009-11-01 2009 11<<<<

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-27 10:14