BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Santha
Pyrite | Level 9

Hi

I would like to do this in SAS. I will get two dates from the user.  For example, the start date is 12/1/2022  and the end date is 12/21/2022 in this example below. After these two inputs, I also get a third input from the user, group#. If the group# is 1, then all dates the dates get a number starting from 1 to whatever is the last number applicable. If the group# is 2, then the first two dates (12/1 and 12/2) in this case below, gets a number of 1,  12/3 and 12/4 gets a number of 2. It is liking clubbing the dates into chunks that is determined by the group#. Group#3 would chunk the dates into set of 3. 12/1, 12/2 and 12/3 would get a value of 1. the next three dates would get a value of 2 so on and so forth. 

Date1_Group2_Group3_Group
12/1/2022111
12/2/2022211
12/3/2022321
12/4/2022422
12/5/2022532
12/6/2022632
12/7/2022743
12/8/2022843
12/9/2022953
12/10/20221054
12/11/20221164
12/12/20221264
12/13/20221375
12/14/20221475
12/15/20221585
12/16/20221686
12/17/20221796
12/18/20221896
12/19/202219107
12/20/202220107
12/21/202221117
1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
data want;
    set have;
    group1+1;
    group2=1+floor((group1-1)/2);
    group3=1+floor((group1-1)/3);
run;
--
Paige Miller

View solution in original post

6 REPLIES 6
PaigeMiller
Diamond | Level 26
data want;
    set have;
    group1+1;
    group2=1+floor((group1-1)/2);
    group3=1+floor((group1-1)/3);
run;
--
Paige Miller
RobPratt
SAS Super FREQ

Besides what @PaigeMiller proposed, here are two alternatives:

data want;
   set have;
   group1 + 1;
   group2 = ceil(group1/2);
   group3 = ceil(group1/3);
run;

data want;
   set have;
   group1 = _N_;
   group2 = ceil(_N_/2);
   group3 = ceil(_N_/3);
run;
Santha
Pyrite | Level 9

thank you Paige and Rob for your suggestions. I get your logic. Will try that out and I am confident that it will work.

Thanks both for your support

Santha
Pyrite | Level 9

 Hi Paige and Rob

Sorry for the confusion. I did not clearly specify what I want . Here is my clarification. My inputs are

(a) lets say these three dates is what I have as my input. I want to chunk ify them. Pls see below.

Date
1/4/2022
1/7/2022
1/8/2022

(b) WindowNumber . Lets say Windownumber=2 (this means i want to chunk my date inputs into a set of 2 timechunks). what i want is a subset of this table below.

DateGroup2
1/4/20221
1/5/20221
1/6/20222
1/7/20222
1/8/20223
1/9/20223
1/10/20224
1/11/20224
1/12/20225

 

That is essentially this below. Essentially want the code to chunk-ify all the dates in between into Windownumbers and just give this small subset below.  

DateGroup2
1/4/20221
1/7/20222
1/8/20223

Please let me know if this is not clear.

RobPratt
SAS Super FREQ
data have;
   input Date mmddyy10.;
   datalines;
1/4/2022
1/7/2022
1/8/2022
;

data want(drop=first);
   set have;
   retain first;
   if _N_ = 1 then first = Date;
   Group2 = 1 + floor((Date-first)/2);
run;

proc print data=want;
   format Date mmddyy10.;
run;
Obs Date Group2
1 01/04/2022 1
2 01/07/2022 2
3 01/08/2022 3
Santha
Pyrite | Level 9

Rob. This is brilliant. Thank you for support as always

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

Multiple Linear Regression in SAS

Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 6 replies
  • 3617 views
  • 1 like
  • 3 in conversation