简体   繁体   English

SAS获取每月的周数

[英]SAS Get week number for each month

I'm new to SAS and have been trying to figure out how to get the week number for each month. 我是SAS的新手,一直在努力弄清楚如何获取每个月的星期数。 I'm having an issue with the months where they don't start at the beginning of the week. 我对几个月不在周初开始的月份有疑问。 For example, if I have a month where the data from the 1st of the month falls on a Thursday, it shows the 1st and 2nd of that month as week 0. Is there a way to display those weeks as week 1? 例如,如果我有一个月,该月的第一天的数据位于星期四,则该月的第一天和第二天显示为第0周。是否可以将这些周显示为第1周? I've tried different things and have been unsuccessful. 我尝试了不同的尝试,但均未成功。

DATA getweek;
set test; 
if year(rundt) ne year(today())then delete;
   month = month(rundt); 
   week1=intck('week',intnx('month',rundt,0),rundt);
   format rundt MMDDYY8.;     
RUN;

This depends largely on how you want to define week number. 这在很大程度上取决于您要如何定义周数。 If the first of the month falls on a wednesday, what week is that week? 如果每月的第一天是星期三,那一周是星期几? Is that week one, or week zero? 那是第一周还是零周? It can be commonly referred to either way. 通常可以以任何一种方式提及。

If you want one, so Thurs/Fri/Sat are week 1, then the 4th is week 1, then just add one to the result - it's consistently going to be off (down) by one, after all. 如果您想要一个,那么周四/周五/周六是第1周,那么第4周是第1周,那么只需将结果加1-毕竟,结果总是会减少1。

data test;
do rundt=19805 to 19904;
day_of_month=day(rundt);
output;
end;
format rundt date9.;
run;

DATA getweek;
set test; 
if year(rundt) ne year(today())then delete;
   month = month(rundt); 
   week1=intck('week',intnx('month',rundt,0),rundt)+1;
   format rundt MMDDYY8.;     
RUN;

Now, if you want the first 9 or 10 days to be week 1, you have a different solution (though I don't recommend this). 现在,如果您希望将第9天或第10天设为第1周,则可以采用其他解决方案(尽管我不建议这样做)。 Use min to set it to minimum of one. 使用min将其设置为最小值1。 This means your first week could be as many as 13 days, which is counterintuitive, but if it's what you need, this is how you do it. 这意味着您的第一周可能多达13天,这违反直觉,但是如果您需要它,这就是您的工作方式。

DATA getweek;
set test; 
if year(rundt) ne year(today())then delete;
   month = month(rundt); 
   week1=min(intck('week',intnx('month',rundt,0),rundt),1);
   format rundt MMDDYY8.;     
RUN;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM