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

Hello,

 

Here's my dataset.

 

I would like to develop some go that will permit to map the date2 at the good value as it is into the dataset, based on date1 and workingday value.  When it is a working day, workingday eq one and if not it equals zero.  

 

So I would like a sas code example to map date2 base on date1 and workday value.

Does someone know how to do that ?

Regards,

data want;
infile datalines delimiter=','; 
input date1: yymmdd10. workingday: 1. date2: yymmdd10.;
format date1 yymmdd10. workingday 1. date2 yymmdd10.;
datalines;
2020-01-01,0,20200103
2020-01-02,0,20200103
2020-01-03,1,20200103
2020-01-04,0,20200106
2020-01-05,0,20200106
2020-01-06,1,20200106
2020-01-07,1,20200107
2020-01-08,1,20200108
2020-01-09,1,20200109
2020-01-10,1,20200110
2020-01-11,0,20200113
2020-01-12,0,20200113
2020-01-13,1,20200113
;
run;

Please note that I would like to have date2 using yymmddn8. format but I have tried it the code and it does not like it.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

So assuming that what you are asking is how to set DATE2 to the NEXT value of DATE1 that is a WORKINGDAY,  then the easiest way is to re-order the data in DESCREASING order of DATE1 and just RETAIN the value.  (Note that for RETAIN to work correctly the variable cannot be on the input dataset, otherwise each time the SET statement executes the retained value is overwritten by the value read in.)

data have;
  input date1 :yymmdd10. workingday ;
  format date1 yymmdd10.;
datalines;
2020-01-01 0
2020-01-02 0
2020-01-03 1
2020-01-04 0
2020-01-05 0
2020-01-06 1
2020-01-07 1
2020-01-08 1
2020-01-09 1
2020-01-10 1
2020-01-11 0
2020-01-12 0
2020-01-13 1
;

proc sort data=have;
  by descending date1;
run;

data want;
  set have;
  by descending date1 ;
  retain date2;
  format date2 yymmdd10.;
  if workingday then date2=date1;
run;

proc print;
run;

Tom_0-1671138206598.png

 

 

 

View solution in original post

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

How about

 

data have;
infile datalines delimiter=','; 
input date1: yymmdd10. workingday: 1.;
format date1 yymmdd10. workingday 1.;
datalines;
2020-01-01,0 
2020-01-02,0 
2020-01-03,1 
2020-01-04,0 
2020-01-05,0 
2020-01-06,1 
2020-01-07,1 
2020-01-08,1 
2020-01-09,1 
2020-01-10,1 
2020-01-11,0 
2020-01-12,0 
2020-01-13,1 
;
run;

data want(drop = d);
   do until (workingday);
      set have;
   end;

   d = date1;

   do until (workingday);
      set have;
      date2 = d;
      output;
   end;

   format date2 yymmddn8.;
run;
ballardw
Super User

What are the rules for "map the date2 at the good value"? I do not see anything that I can follow as to what value is used where.

You should provide an example of the data after the mapping is done.

 

What do you mean by "does not like it" in "Please note that I would like to have date2 using yymmddn8. format but I have tried it the code and it does not like it."

This runs just fine and displays the values of date2 that way:

data want;
infile datalines delimiter=','; 
input date1: yymmdd10. workingday: 1. date2: yymmdd10.;
format date1 yymmdd10. workingday 1. date2 yymmddn8.;
datalines;
2020-01-01,0,20200103
2020-01-02,0,20200103
2020-01-03,1,20200103
2020-01-04,0,20200106
2020-01-05,0,20200106
2020-01-06,1,20200106
2020-01-07,1,20200107
2020-01-08,1,20200108
2020-01-09,1,20200109
2020-01-10,1,20200110
2020-01-11,0,20200113
2020-01-12,0,20200113
2020-01-13,1,20200113
;

There is no INFORMAT of yymmddn8. though.

Tom
Super User Tom
Super User

So assuming that what you are asking is how to set DATE2 to the NEXT value of DATE1 that is a WORKINGDAY,  then the easiest way is to re-order the data in DESCREASING order of DATE1 and just RETAIN the value.  (Note that for RETAIN to work correctly the variable cannot be on the input dataset, otherwise each time the SET statement executes the retained value is overwritten by the value read in.)

data have;
  input date1 :yymmdd10. workingday ;
  format date1 yymmdd10.;
datalines;
2020-01-01 0
2020-01-02 0
2020-01-03 1
2020-01-04 0
2020-01-05 0
2020-01-06 1
2020-01-07 1
2020-01-08 1
2020-01-09 1
2020-01-10 1
2020-01-11 0
2020-01-12 0
2020-01-13 1
;

proc sort data=have;
  by descending date1;
run;

data want;
  set have;
  by descending date1 ;
  retain date2;
  format date2 yymmdd10.;
  if workingday then date2=date1;
run;

proc print;
run;

Tom_0-1671138206598.png

 

 

 

alepage
Barite | Level 11
It seems so simple for you and for me I had no idea how to do that. Sorting by descending order and using a retain statement. It does exactly what I wish.

Thank you very much

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 448 views
  • 0 likes
  • 4 in conversation