BookmarkSubscribeRSS Feed
Saikiran_Mamidi
Obsidian | Level 7

data abc;
input col1 $ col2 col3 col4 $ Col5 $;
datalines;
NA 10 20 NA Hi
NA 11 22 NA NA
NA 12 13 NA ho
NA 16 22 BA jj
10 20 17 NA NA

;

run;

 

so only second row satisfies...

refrain from hard coding...

no hard coding please i.e. mention of columns. this is simple example, what if you had 100 columns;;

8 REPLIES 8
Saikiran_Mamidi
Obsidian | Level 7
can i get the code if possible.many thanks
VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

what have you tried?  everyone wants code to be provided for them without learning......

Coding is more than just taking someone else's code and using it in you requirements. 

ed_sas_member
Meteorite | Level 14

Hi @Saikiran_Mamidi 

 

You can try this:

 

- the array '_char' contains all character variables.

- a counter is initiated (count = 0);

- each time a variable of the array is equal to "NA", 1 is added to the counter

-> so the counter, at the end, contains the number of character variable equal to 'NA'.

Then , the value of this counter is compared to the number of variables in the array (= the dimension of the array represented by dim(array), which is equal to 3 in your example)

data want;
	set abc;
	array _char(*) _character_;
	count = 0;
	do i=1 to dim(_char);
		if _char(i) = "NA" then count+1;
	end;
	if count = dim(_char) then delete;
	drop i count;
run;
Tom
Super User Tom
Super User

Why do the variables have NA in them to begin with?  What if the variable has a valid value of NA? Perhaps it is storing the abbreviation for sodium. Or it has a person's last name.

hashman
Ammonite | Level 13

@Tom: NA = Not Applicable, I'd guess. 

hashman
Ammonite | Level 13

@Saikiran_Mamidi:

This should do it:

data have ;                                                                                                                             
  input (col1-col5) ($) ;                                                                                                               
  cards ;                                                                                                                               
NA 10 20 NA Hi                                                                                                                          
NA 11 22 NA NA                                                                                                                          
NA NA NA NA NA                                                                                                                          
NA 12 13 NA ho                                                                                                                          
NA 16 22 BA jj                                                                                                                          
10 20 17 NA NA                                                                                                                          
;                                                                                                                                       
run ;                                                                                                                                   
                                                                                                                                        
data want ;                                                                                                                             
  set have ;                                                                                                                            
  array col col: ;                                                                                                                      
  if count (cats (of col:), "NA") < dim (col) ;                                                                                         
run ;              

Bear in mind, however, that if combined length of the COL variables should exceed 32767, there would be not enough CAT buffer length to accommodate the internal concatenation. That's why an array approach, though less elegant and more verbose, is more robust:

data want (drop = _:) ;                                                                                                                 
  set have ;                                                                                                                            
  retain _stop "" ;                                                                                                                     
  array col[*] col: _stop ;                                                                                                             
  do _i = 1 by 1 until (col[_i] ne "NA") ;                                                                                              
  end ;                                                                                                                                 
  if _i < dim (col) - 1 ;                                                                                                               
run ;

Note how the sentinel _STOP is made use of to save one key comparison in each iteration of the loop - the trick termed by D. Knuth "quick sequential search".

 

Kind regards

Paul D.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 8 replies
  • 1565 views
  • 1 like
  • 6 in conversation