BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.

Hello All, 
         I would like the community's help on looping around the columns of a data set, check if a column has at least one row with a missing value.

 

data values;
   input @1 var1 3. @5 var2 3.;
   if missing(var1) then
      do;
         put 'Variable 1 is Missing.';
      end;
   else if missing(var2) then
      do;
         put 'Variable 2 is Missing.';
      end;
   datalines;
127
988 195
;
run;

          This is the manual way I can see from Missing() documentation and it would work for small datasets. But I would like the ability to do this over large data sets easily. 

1 ACCEPTED SOLUTION

Accepted Solutions
r_behata
Barite | Level 11

Slightly modified version 

 

data values;
	input @1 var1 3. @5 var2 3.;
	array var[*] var:;
	do i=1 to dim(var);
		if missing(var[i]) then do;
			put "Variable "  i "is missing";
		end;
	end;
drop i ;
datalines;
127
988 195
;
run;

View solution in original post

5 REPLIES 5
mkeintz
PROC Star

 

You can loop through columns by specifying an ARRAY structure (typically a one-dimensional array), where there is one array element for each variables of interest:

 

data values;
   input @1 var1 3. @5 var2 3.;
  array v {2} var1 var2;
  do i=1 to 2;
     if missing(v{I}) then put 'Variable ' I 'is Missing.'  v{I}=;
  end;
datalines;
127
988 195
;
run;

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
UdayGuntupalli
Quartz | Level 8

@mkeintz
        This looks great. However, I would like to request some extra details around the array declaration for my learning: 
1. Why are you manually declaring column names after the declaration of  the array ?  

data values;
   input @1 var1 3. @5 var2 3.; 
  array v {2} var1 var2; * Manually declaring columns here ?; 
  do i=1 to 2;
     if missing(v{I}) then put 'Variable ' I 'is Missing.'  v{I}=;
  end;
datalines;
127
988 195
;
run;

 

2. Is there a way to automatically get all the column names and use it in the loop ?  

mkeintz
PROC Star

@UdayGuntupalli wrote:


        This looks great. However, I would like to request some extra details around the array declaration for my learning: 
1. Why are you manually declaring column names after the declaration of  the array ?  


Because

  1. The array statement needs a way to have correspondence between the array element and the reference variable.  A specific listg of variable names in one way to do this, and you only showed 2 variables.
  2. You didn't specify whether there are other variables not to include in the array.

@UdayGuntupalli@ also wrote:

 

2. Is there a way to automatically get all the column names and use it in the loop ?  


 

By "all the columns names" do you mean all the numeric variables?  If yes, then use
     array v {*} _numeric_ ;
and
    do i=1 to dim(v);

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
r_behata
Barite | Level 11

Slightly modified version 

 

data values;
	input @1 var1 3. @5 var2 3.;
	array var[*] var:;
	do i=1 to dim(var);
		if missing(var[i]) then do;
			put "Variable "  i "is missing";
		end;
	end;
drop i ;
datalines;
127
988 195
;
run;
Ksharp
Super User
ods select none;
ods output nlevels=want;
proc freq data=have nlevels;
table _all_;
run;
ods select all;
proc sql noprint;
select TableVar into : missing_variables separated by ' '
from want
where NNonMissLevels=0;
quit;
%put Missing variables are: &missing_variables ;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 10439 views
  • 2 likes
  • 4 in conversation