Translate SAS If Then Do logic to R

Hello,

I'm recoding numerous SAS applications into R where "If Then Do" logic appears frequently.

Example SAS Code:
if (missing(minority) = 0 AND missing(amin =0 ) then do;
if ( amin = 1 OR hawaii = 1 ) then
other = 1 ;
else other = 0 ;
end;

The R code below works:
recode <- joined_df %>%
mutate(flag_one = case_when(
is.na(minority) & is.na(amin) ~ 9),
other = case_when(
flag_one== 9 & (amin == 1 | hawaii_ == 1) ~ 1, TRUE ~ 0 )
)
I was wondering if there is a more elegant way code to do this in tidyverse so I don't create and then drop temporary flags.

Thanks!

recode <- joined_df %>%
mutate(other = case_when(
is.na(minority) & is.na(amin)  == 9 & (amin == 1 | hawaii_ == 1) ~ 1, 
TRUE ~ 0 ))

?

1 Like

Thank you Nir for your simplified solution! (for others, the ==9 was left over from my solution with a flag variable, just delete it).

Jade

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.