BookmarkSubscribeRSS Feed
dassuz
Calcite | Level 5

Hi I am trying to bring back working days from two columns in SAS I used the following code

 

data Want;
WorkDays_Diff = intck ('Weekday',ReadDate, CreatedDate);
set have;
run;

How ever when i got the results Workdays_Diff comes one row below and theres a dot' .' in the first row. So the results for row 1 is in row 2 and result for row 2 is in row 3 etc

 

If you want to manipulate values after they are read from the data set on the Set statement the calculation should come AFTER the SET statement.

8 REPLIES 8
Krueger
Pyrite | Level 9

Can you share your Have dataset?

dassuz
Calcite | Level 5

 

The Data Set comprises of 550000 rows, I have attached a small sample but I have amended the sensitive data

 

 

dassuz
Calcite | Level 5

Sample Data same as attachment

Src_Sys_Metering_Point_NumMPXN1SAP_Installation_NumMeter_Reading_Doc_IdReadDateMeter_ReadMeter_Reading_Type_CdMeter_Read_RsnCreatedDateCOR_flagDate_Diff
142621426260003733985514/10/201954910CO114/10/201910
142621426260003737595315/09/201954910CO115/09/201910
14262142626000373742715/08/201954910CO115/08/201910
142621426260003737810015/07/201954910CO115/07/201910
124331243360020314073301/10/2019746229I618/10/2019017
124331243360020313050430/09/2019012130/09/201900
87757887757860020177605930/09/2019393301117/10/2019017
87757887757860020175853130/09/2019843861117/10/2019017
87757887757860020178456930/09/2019264803104/10/201904
5627635627636233334606116/10/20190R917/10/201901
5627635627636233337438915/10/2019769603R916/10/201901
5627635627636233333373415/10/20190R916/10/201901
5627635627636233333345215/10/201995563R916/10/201901
5627635627636233333865014/10/2019749403R915/10/201901
5627635627636233337071014/10/201970934R915/10/201901
dassuz
Calcite | Level 5

that is what I used but instead of putting the dates I entered the column dates

unison
Lapis Lazuli | Level 10

It looks like your "date" columns may actually be character columns.

You need to work to convert the "dd/mm/yyyy" to a date format of your choice. Here, I convert to date9.

 

Try something like this:

 

data raw;
	input ReadDate $10. CreatedDate $10.;
	datalines;
14/10/201914/10/2019
15/09/201915/09/2019
15/08/201915/08/2019
15/07/201915/07/2019
01/10/201918/10/2019
30/09/201930/09/2019
30/09/201917/10/2019
30/09/201917/10/2019
30/09/201904/10/2019
16/10/201917/10/2019
;
run;

data want(drop=ReadDate CreatedDate rename=(ReadDate2=ReadDate CreatedDate2=CreatedDate));

	set raw;

	ReadDate = transtrN(ReadDate,"/",trimn(' '));
	CreatedDate = transtrN(CreatedDate,"/",trimn(' '));

	format ReadDate2 CreatedDate2 date9.;
	ReadDate2 = input(ReadDate,ddmmyy10.);
	CreatedDate2 = input(CreatedDate,ddmmyy10.);

	WorkDays_Diff = intck ('Weekday',ReadDate2, CreatedDate2);

run;

-unison

 

-unison
Patrick
Opal | Level 21

@unison 

There is no need to convert the source string. Just use the informat with a defined length.

data _null_;
  dt=input('14/10/2019',ddmmyy10.);
  put dt= date9.;
  stop;
run;
28         data _null_;
29           dt=input('14/10/2019',ddmmyy10.);
30           put dt= date9.;
31           stop;
32         run;

dt=14OCT2019

 

Patrick
Opal | Level 21

@dassuz 

I've used the EG import wizard for the sample data you've posted and added a few more test cases.

To exclude Saturday & Sunday from the calculation I've used interval WEEKDAY17W

Have a look below if the code returns the desired result.

DATA WORK.Sample_Data;
  LENGTH
    Src_Sys_Metering_Point_Num   8
    MPXN1              8
    SAP_Installation_Num   8
    Meter_Reading_Doc_Id   8
    ReadDate           8
    Meter_Read         8
    Meter_Reading_Type_Cd $ 2
    Meter_Read_Rsn     8
    CreatedDate        8
    COR_flag           8
    Date_Diff          8;
  FORMAT
    Src_Sys_Metering_Point_Num BEST12.
    MPXN1            BEST12.
    SAP_Installation_Num BEST12.
    Meter_Reading_Doc_Id BEST12.
    ReadDate         DATE9.
    Meter_Read       BEST12.
    Meter_Reading_Type_Cd $CHAR2.
    Meter_Read_Rsn   BEST12.
    CreatedDate      DATE9.
    COR_flag         BEST12.
    Date_Diff        BEST12.;
  INFORMAT
    Src_Sys_Metering_Point_Num BEST12.
    MPXN1            BEST12.
    SAP_Installation_Num BEST12.
    Meter_Reading_Doc_Id BEST12.
    ReadDate         DATE9.
    Meter_Read       BEST12.
    Meter_Reading_Type_Cd $CHAR2.
    Meter_Read_Rsn   BEST12.
    CreatedDate      DATE9.
    COR_flag         BEST12.
    Date_Diff        BEST12.;
  INFILE DATALINES4
    DLM='7F'x
    MISSOVER
    DSD;
  INPUT
    Src_Sys_Metering_Point_Num : BEST32.
    MPXN1            : BEST32.
    SAP_Installation_Num : BEST32.
    Meter_Reading_Doc_Id : BEST32.
    ReadDate         : BEST32.
    Meter_Read       : BEST32.
    Meter_Reading_Type_Cd : $CHAR2.
    Meter_Read_Rsn   : BEST32.
    CreatedDate      : BEST32.
    COR_flag         : BEST32.
    Date_Diff        : BEST32.;
  DATALINES4;
14262142626000373398552183654910CO12183610
14262142626000373759532180754910CO12180710
1426214262600037374272177654910CO12177610
14262142626000373781002174554910CO12174510
124331243360020314073321823746229I621840017
12433124336002031305042182201212182200
87757887757860020177605921822393301121839017
87757887757860020175853121822843861121839017
8775788775786002017845692182226480312182604
56276356276362333346061218380R92183901
5627635627636233337438921837769603R92183801
56276356276362333333734218370R92183801
562763562763623333334522183795563R92183801
5627635627636233333865021836749403R92183701
562763562763623333707102183670934R92183701
;;;;

/* add more test cases */
data WORK.Sample_Data;
  set WORK.Sample_Data(keep=ReadDate CreatedDate) end=last;
  output;

  /* add more test cases */
  if last then
    do;
      ReadDate='01Nov2019'd;
      CreatedDate='01Nov2019'd;
      do j=0 to 6;
        ReadDate=ReadDate+j;
        do i=0 to 10;
          CreatedDate=ReadDate+i;
          output;
        end;
      end;
    end;
run;

data want;
  set WORK.Sample_Data;
  format ReadDate CreatedDate weekdate.;
  format WorkDays_Diff 32.;
  format ReadDate_MonToFri CreatedDate_MonToFri weekdate.;
  ReadDate_MonToFri=intnx('WEEKDAY17W',ReadDate,0);
  CreatedDate_MonToFri=intnx('WEEKDAY17W',CreatedDate,0);
  WorkDays_Diff=intck ('Weekday',ReadDate, CreatedDate);
run;

proc print;
run;

Capture.JPG

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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
  • 8 replies
  • 1738 views
  • 3 likes
  • 5 in conversation