SASnR Home

Arrays - Example - replace a value in all numeric and character variables


SAS

data alldata;
infile datalines dlm='|' dsd missover;
input var1 : best32. var2 : best32. var3 : best32. var4 : best32. var5 : best32. var6 : $10. var7 : $10. var8 : $10. var9 : $10. var10 : $10.;
label ;
format ;
datalines4;
9909|1|2|3|4|_missing_|text1|text2|_missing_|text3
5|9909|6|7|8|text4|text5|_missing_|text6|text7
9|3|9909|2|7|text8|_missing_|text9|text10|text11
;;;;
run;


data alldata01;
    set alldata;

    *------------------------------------------------------------------------------;
    *character variables;
    *------------------------------------------------------------------------------;

    array xchar[*] _character_;

    do i=1 to dim(xchar);
        if lowcase(xchar[i])="_missing_" then xchar[i]="";
    end;

    *------------------------------------------------------------------------------;
    *numeric variables;
    *------------------------------------------------------------------------------;

    array xnum[*] _numeric_;

    do j=1 to dim(xnum);
        if xnum[j]= 9909 then xnum[j]=0;
    end;

    drop i j;

run;

 

Description:

  • It creates a new dataset named alldata01.
  • It uses the set statement to read data from an existing dataset named alldata into the new dataset.
  • The code defines an array named xchar to hold the character variables in the dataset.
  • Within a do loop, it iterates over each element of the xchar array using the index variable i.
  • Inside the loop, it checks if the lowercase value of each character variable (lowcase(xchar[i])) is equal to "missing". If the condition is true, it replaces the value with an empty string (xchar[i] = "").
  • The code then defines an array named xnum to hold the numeric variables in the dataset.
  • Within another do loop, it iterates over each element of the xnum array using the index variable j.
  • Inside the loop, it checks if the value of each numeric variable (xnum[j]) is equal to 9909. If the condition is true, it replaces the value with 0 (xnum[j] = 0).
  • Finally, the drop statement is used to remove the index variables i and j from the dataset.
  • The run statement signifies the end of the data step.

R tidyverse


 

library(tidyverse)

alldata<-tribble(
~var1,~var2,~var3,~var4,~var5,~var6,~var7,~var8,~var9,~var10,
9909,1,2,3,4,"_missing_","text1","text2","_missing_","text3",
5,9909,6,7,8,"text4","text5","_missing_","text6","text7",
9,3,9909,2,7,"text8","_missing_","text9","text10","text11",
)


alldata01 <- alldata %>%
  mutate(
    across(where(is.character), ~ if_else(tolower(.x) == "_missing_", "", .x)),
    across(where(is.numeric), ~ if_else(.x == 9909, 0, .x))
  )

  

 

Description:

  • It creates a new dataset named alldata01 by applying transformations to an existing dataset named alldata.
  • The mutate function is used to modify the dataset columns.
  • The across function is used to specify the columns to be modified.
  • The where function is used within across to select columns based on their data type (is.character and is.numeric).
  • For character columns, the if_else function is applied to replace values. It checks if the lowercase value of each character column (tolower(.x)) is equal to "missing". If the condition is true, it replaces the value with an empty string (""), otherwise, it keeps the original value ( .x).
  • For numeric columns, another if_else function is applied to replace values. It checks if the value of each numeric column ( .x) is equal to 9909. If the condition is true, it replaces the value with 0, otherwise, it keeps the original value ( .x).
  • The modifications are applied to the specified columns using the %>% pipe operator.
  • The resulting dataset, alldata01, contains the modified data with the specified replacements.
 

 


SASnR Home