3

I have a SAV file with 5 variables. in two of them I have NA values (written as string. I need to convert NA to "empty value"

thought of using "if then rename" options but with no success as it prints out only one observation or keeps the NA values as they are. please assist.

data QUS;
if happy=NA then rename happy="";
if educ=NA then rename educ="";
run;
proc print data=QUS;
run;
Joe
  • 62,789
  • 6
  • 49
  • 67
user3581800
  • 317
  • 2
  • 5
  • 16

2 Answers2

7

You are close.

In SAS you need to put strings in quotes. (Your code compares if variables Happy and NA are the same but it's not working because you don't have variable named NA).

Rename is for renaming variable names.

Also, you need to specify your dataset in the set statmement.

This will create data QUS which is identical to SAV but fo happy and educ NA is replaced with missing value.

data QUS;
    set SAV;
    if happy="NA" then happy="";
    if educ="NA" then educ="";
run;

proc print data=QUS;
run;
Pekka
  • 2,348
  • 2
  • 21
  • 33
3

Here is an example to change all character or numeric values using ARRAYS.
Something similar to this answer is in this SO answer and a SAS Communities answer HERE.

data HAVE;
    Length id $5 var1 var2 var3 var4 var5 $15;
    Input id $ var1 $ var2 $ var3 $ var4 $ var5 $ num1 num2 num3;
    datalines;
00001 NA VALUE NA VALUE NA . 1 .
00002 VALUE . VALUE NA VALUE 1 . 1
00003 NA VALUE NA VALUE NA . 1 .
00004 VALUE . VALUE NA VALUE 1 . 1
00005 NA VALUE NA VALUE NA . 1 .
;
Run;

data WANT;
    set HAVE; 
    array CHAR _character_ ; 
    array NUM _numeric_ ;
do over CHAR; 
    if CHAR="NA" then call missing(CHAR);
    else if missing(CHAR) then CHAR="WAS MISSING";
end;
do over NUM; 
    if NUM=1 then call missing(NUM);
    else if missing(NUM) then NUM=0;
end;
run ;
Community
  • 1
  • 1
Jay Corbett
  • 28,091
  • 21
  • 57
  • 74