简体   繁体   English

如何计算 SAS 中每月第一周的天数。如果我们将星期六视为第一周

[英]How to calculate the number of days of first week of the month in SAS.if we consider Saturday as first week day

I am using intnx function.我正在使用 intnx function。

date1=intnx('qtr',"01APR2021"d,0,'b');

I am thinking of following algo-我正在考虑遵循算法-

1.Check if last month's last day is not Friday. 1.检查上个月的最后一天是否不是星期五。

2.Calculate the number of days till Friday appears(Current Month). 2.计算直到星期五出现的天数(当前月份)。

Can anyone help me with this?谁能帮我这个?

Use INTNX to compute the 1st of the month, and then WEEKDAY of that to compute the number of days in the first week.使用INTNX计算当月的第一天,然后使用WEEKDAY计算第一周的天数。

WEEKDAY function results are WEEKDAY function 结果是

             1st-want  1st_want-v2
1 Sunday     1         2
2 Monday     2         3
3 Tuesday    3         4
4 Wednesday  4         5
5 Thursday   5         6
6 Friday     6         7
7 Saturday   7         1

Depending on what you want, (suppose the 1st is a Saturday, do you want 1 or 7 for your result?) For 1, you will need to perform additional calculations.取决于你想要什么,(假设第一个是星期六,你想要 1 还是 7 作为你的结果?)对于 1,你需要执行额外的计算。

Example:例子:

Presume if first is a Saturday you want 7 days in first week result.假设 first 是星期六,您希望在第一周结果中保留 7 天。

data have;
  call streaminit (2021);
  do _n_ = 0 to 23;
    date = intnx('month', '01-jan-2019'd, _n_) + rand('integer', 27);
    output;    
  end;
  format date date11.;
run;

data want;
  set have;
  month_1st = intnx('month', date, 0);
  sas_weekday_1st = weekday(month_1st);

  result = sas_weekday_1st;
  result_v2 = mod(sas_weekday_1st,7) + 1;

  format month_1st date11.;
run;

  1. Find the first of the month (INTNX() or MDY())查找当月的第一天(INTNX() 或 MDY())
  2. Find the first friday (day=6) of the month (NWKDOM())查找本月的第一个星期五 (day=6) (NWKDOM())
  3. Subtract减去

Assume month and year are provided.假设提供了月份和年份。

data want;
  year = 2020;
  month = 1;
  start_date = mdy(month, 1, year);
  first_friday = nwkdwom(1, 6, month, year);
  length_week = start_date - first_friday + 1;

  *Toms correction:;
  length_week2 = day(nwkdom(1,6,month(date),year(date));
run;
  

Sounds like you want to know the date of the first Friday in the month.听起来您想知道该月第一个星期五的日期。 And hence the day of the month of the first Friday in the month.因此该月的第一个星期五是该月的一天。

So let's start with a dataset that has a month that starts on each day of the week.因此,让我们从一个数据集开始,该数据集的月份从一周中的每一天开始。

data example;
  input date :date9.;
  format date date9.;
  dow = weekday(date);
  downame=put(date,downame.-l);
cards;
01OCT2000
01MAY2000
01FEB2000
01MAR2000
01JUN2000
01SEP2000
01JAN2000
;

Now we can use the INTNX() function with the WEEK interval to find the date of the first Friday.现在我们可以使用带有 WEEK 间隔的 INTNX() function 来查找第一个星期五的日期。 We could then use the DAY() function to find the day of the month for that Friday and hence how many days are in the first week of the month.然后,我们可以使用 DAY() function 来查找该星期五的月份日期,从而找出该月第一周的天数。

To get the first Friday find the Saturday at the end of the week containing the second day of the month and subtract one to move back to Friday.要获得第一个星期五,请找到包含该月第二天的周末的星期六,然后减去一个以返回星期五。

data want;
  set example;
  friday = intnx('week',date+1,0,'e')-1 ;
  want = day(friday);
  want_name=put(friday,downame.-l);
  format friday date9.;
run;

Results:结果:

Obs         date    dow    downame         friday    want    want_name

 1     01OCT2000     1     Sunday       06OCT2000      6     Friday
 2     01MAY2000     2     Monday       05MAY2000      5     Friday
 3     01FEB2000     3     Tuesday      04FEB2000      4     Friday
 4     01MAR2000     4     Wednesday    03MAR2000      3     Friday
 5     01JUN2000     5     Thursday     02JUN2000      2     Friday
 6     01SEP2000     6     Friday       01SEP2000      1     Friday
 7     01JAN2000     7     Saturday     07JAN2000      7     Friday

To do it with any day in the month use the INTNX() function with MONTH interval to find the first day of the month (and add one to find the second day of the month).要在一个月中的任何一天执行此操作,请使用具有 MONTH 间隔的 INTNX() function 来查找该月的第一天(并添加一个以查找该月的第二天)。

length_week1=day(intnx('week',intnx('month',date,0,'b')+1,0,'e')-1);

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

相关问题 按周分组并将星期六视为一周的开始日 - Group by week and consider Saturday as start day of the week 使用MS SQL计算月份的星期数,星期日作为开始日期,星期六作为结束日期 - Calculate week number of the month, Sunday as Starting and Saturday as ending day Using MS SQL TSQL-如何确定给定星期数的一周的第一天和最后一天 - TSQL - how to determine first and last days of the week for a given week number 将周数转换为周的第一天 - Convert Week Number to First day of Week 假设每月的第一个星期一是去年该月的第一个星期一,如何计算上一年的同一天 - How to calculate previous year same day of week from month suppose first Monday of month would be last year first Monday of that month 如何获得一个月中每周的第一天和最后一天? - How to get first day and last day for every week in a month? 如何在Oracle中生成一周的第一天、一周的最后一天和两个日期之间的周数 - How to generate the first day of the week, the last day of the week and week number between two dates in Oracle 本周的第一天和上周 - First day Of this week and last week 使用存储过程将星期一计算为一周的第一天 - Calculate Monday as first day of the week with a stored procedure Power BI-您如何计算每周的第一天? - Power BI - How do you calculate first Day of Week?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM