mercredi 1 août 2018

Transform if statement in SAS to R

I would like to change a IF statement in SAS to R but i have some problems. In SAS i have this code below :

data new_data;
set my_data;
format end_date date8.;
by my_id;
retain previous_end_date "01JAN1900"d ;
if first.my_id then previous_end_date = end_date ;
else if start_date <= previous_end_date and previous_end_date <= end_date then do; 
available = "NO"; previous_end_date = end_date; end;
else if start_date <= previous_end_date and previous_end_date > end_date then 
do; available ="NO"; end;
else previous_end_date = end_date;
run;

So in R i tried this :

my_data <- my_data %>% group_by(my_id) %>% mutate(rank = row_number()) %>% as.data.frame()

This is a part of my dataframe

           my_id       type start_date   end_date rank
1      02000lfo0   10_Illim 2018-05-15 2018-05-25    1
2      02001zjtu   15_Class 2018-05-08 2018-06-02    1
3      02002akb5   25_Class 2018-05-07 2018-07-06    1
4      02002akdy   05_Illim 2018-05-06 2018-05-13    1
5      02002akdy   05_Illim 2018-05-16 2018-05-23    2
6      02002akdy   20_Illim 2018-05-24 2018-06-23    3
7      02002cz12    25_Data 2018-05-07 2018-06-06    1
8      02002db80    10_EUMG 2018-05-17 2018-05-31    1
9      02002de79   25_Class 2018-05-01 2018-06-30    1
10     02002dlmi    10_Data 2018-05-02 2018-05-10    1
11     02002dlmi   10_Class 2018-05-02 2018-05-15    2
12     02002dlmi   10_Class 2018-05-03 2018-05-16    3
13     02002dlmi    10_Data 2018-05-05 2018-05-13    4
14     02002dlmi    10_Data 2018-05-06 2018-05-14    5
15     02002dlmi    10_Data 2018-05-07 2018-05-15    6
16     02002dlmi    10_Data 2018-05-08 2018-05-16    7
17     02002dlmi    10_Data 2018-05-09 2018-05-17    8
18     02002dlmi   10_Class 2018-05-09 2018-05-22    9
19     02002dlmi   10_Class 2018-05-11 2018-05-24   10
20     02002dlmi    10_Data 2018-05-12 2018-05-20   11
21     02002dlmi    10_Data 2018-05-15 2018-05-23   12
22     02002dlmi    10_Data 2018-05-16 2018-05-24   13
23     02002dlmi   10_Illim 2018-05-17 2018-05-27   14




if (my_data$rank== 1){
  my_data$previous_end_date == my_data$end_date
} else if (my_data$start_date <= my_data$previous_end_date & my_data$previous_end_date <= my_data$end_date) {
  my_data$available== "NON"
} else if (my_data$start_date <= my_data$previous_end_date & my_data$previous_end_date > my_data$end_date) {
  my_data$available="NO"
} else { my_data$previous_end_date == my_data$end_date}

But this returns an error :

Warning messages:

1: In if (my_data$rank == 1) { : the condition has length > 1 and only the first element will be used

2: Unknown or uninitialised column: 'previous_end_date'.

Do you have an idea to rewrite this SAS code in R ?

Aucun commentaire:

Enregistrer un commentaire