楼主: LiouPerng
4159 4

[原创博文] 如何使用宏循环产生连续的yyyymm月份文本 [推广有奖]

  • 0关注
  • 1粉丝

初中生

85%

还不是VIP/贵宾

-

威望
0
论坛币
1279 个
通用积分
0
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
130 点
帖子
15
精华
0
在线时间
12 小时
注册时间
2012-6-22
最后登录
2023-9-27

楼主
LiouPerng 发表于 2012-6-22 11:15:48 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币
  需求是这样的:数据源在多张数据集中,表名大约是dat_yyyymm,即201111、201112、201201、201202……
我需要做一项功能,在用户设置了开始日期、结束日期,比如201111至201202,然后合并这些表中指定条件的指定字段,那么我可以:
select &retain.
from (
    select &retain. from dat_201111 where &condition.
    union all
    select &retain. from dat_201112 where &condition.
    union all
    select &retain. from dat_201201 where &condition.
    union all
    select &retain. from dat_201202 where &condition.
)
group by ...
如果用宏循环该如何做到生成连续的含有yyyymm月份的文本(且中间不能出现分号啊)呢?
二维码

扫码加我 拉你入群

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

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

关键词:如何使用 宏循环 condition Select RETAIN 数据源 where 如何 用户

沙发
foxalife 发表于 2012-6-22 11:21:21

藤椅
playmore 发表于 2012-6-28 08:57:53
帮你写了一个生成年月列表的代码
  1. data date;
  2.         input yy  mm;
  3. datalines;
  4. 2010 1
  5. ;
  6. run;

  7. data date1;
  8.         set date;
  9.         if _N_=1 then output;
  10.         do until(yy=2012 and mm=12);
  11.                 if mm<12 then do;
  12.                         yy=yy;
  13.                         mm=mm+1;
  14.                         output;
  15.                 end;
  16.                 else do;
  17.                         yy=yy+1;
  18.                         mm=1;
  19.                         output;
  20.                 end;
  21.         end;
  22. run;
复制代码
playmore邀请您访问ChinaTeX论坛!!!进入ChinaTeX论坛

板凳
mymine 发表于 2012-6-28 12:22:08
生成了一个宏变量 &time
就是你所要求的连续月份,且中间位空格分开的字段

data a;
do i=1900 to 2050;
do j=1 to 12;
  output;
  end;
end;
run;

data a;
set a;
if j>=10 then month=compress(i||j);
else month=compress(i||'0'||j);
run;

data b;
set a;
where month between '200901' and '201012';
run;
data b;
set b;
format time $10000.;
if _n_=1 then do; time=month; retain time; end;
else time=compress(time||'*'||month);
a='z';
run;
data b;
set b;
by a;
if last.a;
time=tranwrd(time,'*',' ');
keep time;
run;

proc sql noprint;
select time into: time
from b;
quit;

报纸
LiouPerng 发表于 2012-7-15 08:04:10
  1. %macro yyyymm_range(s_yyyymm,e_yyyymm);
  2. %local yyyymm;
  3. %let yyyymm=&s_yyyymm.;
  4. %do %while(&yyyymm.<=&e_yyyymm.);
  5. &yyyymm.
  6.     %if %substr(&yyyymm.,5,2)=12 %then %let yyyymm=%eval((%substr(&yyyymm.,1,4)+1)*100+1);
  7.     %else %let yyyymm=%eval(&yyyymm.+1);
  8. %end;
  9. %mend;

  10. %macro get_months(s_yyyymm,e_yyyymm);
  11. %local s_yyyy e_yyyy s_mm e_mm months;
  12. %let s_yyyy=%substr(&s_yyyymm.,1,4);
  13. %let e_yyyy=%substr(&e_yyyymm.,1,4);
  14. %let s_mm=%substr(&s_yyyymm.,5,2);
  15. %let e_mm=%substr(&e_yyyymm.,5,2);
  16. %let months=%eval((&e_yyyy.-&s_yyyy.)*12+(&e_mm.-&s_mm.)+1);
  17. &months.
  18. %mend;

  19. %let months=%yyyymm_range(201111,201202);
  20. %do i=1 %to %get_months(201111,201202);
  21.     %let yyyymm=%scan(&months.,&i.);
  22.     %put &yyyymm.;
  23. %end;
复制代码

我自己写的宏方案,更具通用性。

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

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