BookmarkSubscribeRSS Feed
podarum
Quartz | Level 8

Hi,  I have data with a date (closing_date) spanning out to 120 days.. I'm trying to group them by different Thursday to Wednesday groupings and name them like "March-10-22 - March-16-22'... and show about 17 groupings each one with a Thr-Wed title..

Thanks

4 REPLIES 4
yabwon
Onyx | Level 15

I would try something like:

data have;
  do closing_dat = today() - 60 to today() + 60;
    output;
  end;
  format closing_dat yymmdd10.;
run;

%let last_day=4; /* 4 = Thursday */
%let format = worddate12.;

data want;
  do _N_ = 1 by 1 until(weekday(closing_dat) = &last_day. or EOF);
    set have end=EOF;

    if _N_=1 then dt = closing_dat;

    if weekday(closing_dat) = &last_day. or EOF then
      label = catx(" - ",put(dt,&format.),put(closing_dat,&format.));
  end;

  do _N_ = 1 to _N_;
    set have;
    output;
  end;
  drop dt;
run;

 

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



podarum
Quartz | Level 8
Thank you , this is great
ballardw
Super User

My take:

data example;
   somedate = '15Feb2022'd;
   do closing_date= somedate to (somedate+120);
      weekstart = intnx('week.5',closing_date,0,'B');
      weekend  = intnx('week.5',closing_date,0,'E');
      text = catx('-',put(weekstart,worddate12.),put(weekend,worddate12.));
      output;
   end;

   format closing_date date9. weekstart weekend date9.;
run; 

The INTNX function can use offsets to define the boundary of the interval.

I show creating some text as requested but I would, if this were my project stick with the WEEKSTART variable and not bother showing the end unless I went to considerable work to use a function to create a possibly obnoxious format. Why would I do that? Because none of the text formats for dates except YYYYMMDD types sort at all correctly and as soon as you go to make a table with that text your going to have something like

Apr 14  2022

Apr 21 2022

Apr 28 2022

Then Feb

Then Jun

Then Mar

Then May

(for example with 120 days starting in Feb)

PaigeMiller
Diamond | Level 26

Although I haven't worked through all the details, I believe you can create a custom format using PROC FCMP to accomplish this. Here's a similar example: https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.4/proc/p1gg77jyhc9s42n1f1vjyx2hsg8b.htm

--
Paige Miller

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 919 views
  • 1 like
  • 4 in conversation