BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
RandyStan
Fluorite | Level 6

 Dear All:

 

May data is as follows

 

ID_A             ID_B             Date                          VAR_A

1                   100              03FEB2020                        -5

1                   100              06FEB2020                         3

1                   100              11FEB2020                          0

1                    100             14FEB2020                          0

1                    100              18FEB2020                        10

1                    100              24FEB2020                        10

2                    100              28JAN2020                         0

2                    100              29JAN2020                         0

2                    100              31JAN2020                        -2

2                    200              05FEB2020                        0

2                    200              12FEB2020                        5

 

I want to construct VAR_B which is the Work Days to the next VAR_A = 0

So my data set should look like        

ID_A             ID_B             Date                          VAR_A                    VAR_B

1                   100              03FEB2020                        -5                        3

1                   100              06FEB2020                         3                        7

1                   100              11FEB2020                          0

1                    100             14FEB2020                          0

1                    100              18FEB2020                        10                       4

1                    100              24FEB2020                        10                   -9999  /*The last observation of each group is -9999 */

2                    100              28JAN2020                         0

2                    100              29JAN2020                         0

2                    100              31JAN2020                        -2                        8

2                    200              05FEB2020                        0

2                    200              12FEB2020                       -5                    -9999  /*The last observation of each group is -9999 */

 

Thanx in advance

Randy

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

This will get the WEEKDAYs between the dates:

 

data have;
input ID_A ID_B Date :date9. VAR_A;
format date yymmdd10.;
datalines;
1                   100              03FEB2020                        -5
1                   100              06FEB2020                         3
1                   100              11FEB2020                          0
1                    100             14FEB2020                          0
1                    100              18FEB2020                        10
1                    100              24FEB2020                        10
2                    100              28JAN2020                         0
2                    100              29JAN2020                         0
2                    100              31JAN2020                        -2
2                    200              05FEB2020                        0
2                    200              12FEB2020                        5
;

proc sort data=have; by ID_A descending date; run;

data want;
do until(last.ID_A);
    set have; by ID_A;
    if first.id_a then var_b = -9999;
    else do;
        if var_a ne 0 and not missing(start) then 
            var_b = intck("weekday", date, start);
        else call missing(var_b);
        end;
    output;
    if var_a ne 0 then start = date;
    end;
drop start;
run;

proc sort data=want; by id_a date; run;

proc print data=want noobs; run;
ID_A 	ID_B 	Date 	VAR_A 	var_b
1 	100 	2020-02-03 	-5 	3
1 	100 	2020-02-06 	3 	8
1 	100 	2020-02-11 	0 	.
1 	100 	2020-02-14 	0 	.
1 	100 	2020-02-18 	10 	4
1 	100 	2020-02-24 	10 	-9999
2 	100 	2020-01-28 	0 	.
2 	100 	2020-01-29 	0 	.
2 	100 	2020-01-31 	-2 	8
2 	200 	2020-02-05 	0 	.
2 	200 	2020-02-12 	5 	-9999

To get the workdays, excluding holidays, I think you will need to use custom intervals .

PG

View solution in original post

9 REPLIES 9
PGStats
Opal | Level 21

Please explain VAR_B=4 here, where there is no posterior VAR_A=0 observation in the ID=1 group

 

ID_A ID_B Date VAR_A VAR_B

1 100 03FEB2020 -5 3

1 100 06FEB2020 3 7

1 100 11FEB2020 0

1 100 14FEB2020 0

1 100 18FEB2020 10 4

1 100 24FEB2020 10

2 100 28JAN2020 0

PG
RandyStan
Fluorite | Level 6

Dear PGStats

18FEB2020 to 24FEB2020 is 4 working days -- 19,20,21,24.

Thanks so much

PaigeMiller
Diamond | Level 26

Explain VAR_B=3 on the first row. There are 8 days to the place where VAR_A=0, so even if you exclude Saturdays and Sundays, you don't get 3.

--
Paige Miller
RandyStan
Fluorite | Level 6

Dear Mr. Miller:

Let me explain the logic. 

03FEB2020 to 06FEB2020 is 3 work days -- hence the 3 (days)

Because VAR_A  = 0 on 11FEB2020 and 14FEB2020 the next day to be considered is 18FEB2020 is 7 workdays (17FEB2020) was a holiday.

And so forth.

If you need any other clarification please do not hesitate.

Thanx so much.

Randy

PGStats
Opal | Level 21

So, your statement should read

 

VAR_B which is the number of Work Days to the next observation where VAR_A ne 0 ?

PG
RandyStan
Fluorite | Level 6

Yes.

Apologies.

PGStats
Opal | Level 21

This will get the WEEKDAYs between the dates:

 

data have;
input ID_A ID_B Date :date9. VAR_A;
format date yymmdd10.;
datalines;
1                   100              03FEB2020                        -5
1                   100              06FEB2020                         3
1                   100              11FEB2020                          0
1                    100             14FEB2020                          0
1                    100              18FEB2020                        10
1                    100              24FEB2020                        10
2                    100              28JAN2020                         0
2                    100              29JAN2020                         0
2                    100              31JAN2020                        -2
2                    200              05FEB2020                        0
2                    200              12FEB2020                        5
;

proc sort data=have; by ID_A descending date; run;

data want;
do until(last.ID_A);
    set have; by ID_A;
    if first.id_a then var_b = -9999;
    else do;
        if var_a ne 0 and not missing(start) then 
            var_b = intck("weekday", date, start);
        else call missing(var_b);
        end;
    output;
    if var_a ne 0 then start = date;
    end;
drop start;
run;

proc sort data=want; by id_a date; run;

proc print data=want noobs; run;
ID_A 	ID_B 	Date 	VAR_A 	var_b
1 	100 	2020-02-03 	-5 	3
1 	100 	2020-02-06 	3 	8
1 	100 	2020-02-11 	0 	.
1 	100 	2020-02-14 	0 	.
1 	100 	2020-02-18 	10 	4
1 	100 	2020-02-24 	10 	-9999
2 	100 	2020-01-28 	0 	.
2 	100 	2020-01-29 	0 	.
2 	100 	2020-01-31 	-2 	8
2 	200 	2020-02-05 	0 	.
2 	200 	2020-02-12 	5 	-9999

To get the workdays, excluding holidays, I think you will need to use custom intervals .

PG
RandyStan
Fluorite | Level 6
Thank you so much
PaigeMiller
Diamond | Level 26

@RandyStan, typographical errors aside, your explanation still is unclear.

 

In row 2, you show VAR_B should be 7, even though there is only 5 days between that line and the next line. Where does that number 7 come from?

 

Can you please write a complete and clear description so we don't have to scroll up and down and piece together different parts of your explanation into a whole?

--
Paige Miller

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 9 replies
  • 1187 views
  • 2 likes
  • 3 in conversation