DATA Step, Macro, Functions and more

Missing variables

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 10
Accepted Solution

Missing variables

Hello

How can i get the name of all missing variables raw by raw. thx

 

example

 

var1   var2   var3   var4    deriver_var  

.          1        2        3       var1

2         .        .          4       var2 var3

.          .        4         .       var1 var2 var4


Accepted Solutions
Solution
‎05-17-2017 08:29 AM
PROC Star
Posts: 7,468

Re: Missing variables

data want;
  set have;
  length deriver_var $50;
  array vars(*) var1-var4;
  do _n_=1 to dim(vars);
    if missing(vars(_n_)) then do;
      deriver_var=catx(' ',deriver_var,vname(vars(_n_)));
    end;
  end;
run;

Art, CEO, AnalystFinder.com

 

View solution in original post


All Replies
Solution
‎05-17-2017 08:29 AM
PROC Star
Posts: 7,468

Re: Missing variables

data want;
  set have;
  length deriver_var $50;
  array vars(*) var1-var4;
  do _n_=1 to dim(vars);
    if missing(vars(_n_)) then do;
      deriver_var=catx(' ',deriver_var,vname(vars(_n_)));
    end;
  end;
run;

Art, CEO, AnalystFinder.com

 

Occasional Contributor
Posts: 10

Re: Missing variables

Thanks

PROC Star
Posts: 283

Re: Missing variables

@Raffik Please mark @art297 's solution as accepted and answered. Thank you!

Contributor
Posts: 33

Re: Missing variables

Dear Art,

how can I modify the code to get the deriver_var in a data set with several numeric and character variables like the attached example?

 

all your help will be appreciated

 

Thanks

Ed

 

 

 

PROC Star
Posts: 7,468

Re: Missing variables

I was unable to view your workbook, but here is one way to include both character and numeric variables:

data class;
  set sashelp.class;
  if _n_ in (2,8) then do;
    call missing(age);
    call missing(sex);
    call missing(weight);
  end;
run;

data want;
  set class;
  length deriver_var $50;
  array cvars(*) name sex;
  array nvars(*) age height weight;
  do _n_=1 to dim(cvars);
    if missing(cvars(_n_)) then do;
      deriver_var=catx(' ',deriver_var,vname(cvars(_n_)));
    end;
  end;
  do _n_=1 to dim(nvars);
    if missing(nvars(_n_)) then do;
      deriver_var=catx(' ',deriver_var,vname(nvars(_n_)));
    end;
  end;
run;

You could specify the numeric variables with _numeric_ rather than spelling them out. Unfortunately, you can't do the same with the character variables as deriver_var will be included in the list of character variables.

 

Art, CEO, AnalystFinder.com

 

Contributor
Posts: 33

Re: Missing variables

Hi Art,
Thanks for the code, unfortunately I have a data set with lots of numeric
and character variables, is there a way to include all of them in the code?


Here is what I intent to get:

I have:
id young home tree garden garage livingroom
1 2 8 yes 4 8
2 4 6 no
3 yes 8 8 4 1 7
4 yes 9 1 5
5 7 5 0 7 2 3
6 8 no 4 5 9
7 3 4 7 7 7
8 7 8 8
9 5 no 8 9
10 8 6 4 7 no 10
Would like to have:
Id deriver_var
1 garage
2 home garden livingroom
4 garden livingroom
6 home
7 young
8 young garden livingroom
9 tree garage


Thanks
Ed.


##- Please type your reply above this line. Simple formatting, no
attachments. -##
PROC Star
Posts: 7,468

Re: Missing variables

You could use _character_ and just exclude the new variable from the check. e.g.:

data class;
  set sashelp.class;
  if _n_ in (2,8) then do;
    call missing(age);
    call missing(sex);
    call missing(weight);
  end;
run;

data want;
  set class;
  length deriver_var $50;
  array cvars(*) _character_;
  array nvars(*) _numeric_;
  do _n_=1 to dim(cvars);
    if vname(cvars(_n_)) ne 'deriver_var' and missing(cvars(_n_)) then do;
      deriver_var=catx(' ',deriver_var,vname(cvars(_n_)));
    end;
  end;
  do _n_=1 to dim(nvars);
    if missing(nvars(_n_)) then do;
      deriver_var=catx(' ',deriver_var,vname(nvars(_n_)));
    end;
  end;
run;

Art, CEO, AnalystFinder.com

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 269 views
  • 3 likes
  • 4 in conversation