BookmarkSubscribeRSS Feed
Satori
Quartz | Level 8

I'm running this code:

 

data want;
   set have end=eof;
   array vars v1-v58;
   array nonmiss[58];
   do index=1 to dim(vars) while (vars[index]);
   nonmiss[index]+1;
 end;
   if eof;
   keep nonmiss:;

proc print data=work.nm;

 

I get the following error:

ERROR: All variables in array list must be the same type, i.e., all numeric or character.

 

How can I run this code with both numeric and character variables?

8 REPLIES 8
PaigeMiller
Diamond | Level 26

@Satori wrote:

 

How can I run this code with both numeric and character variables?


Change your array statement so it only has numeric vars. Run the code. Change the array statement so it only has character vars. Run the code again.

 

Or better yet, fix the data so var1-var58 are all numeric (or all character).

--
Paige Miller
Satori
Quartz | Level 8

I converted all character vars to numeric, but some variables are text so they don't convert.

Then I converted all numeric to character, but now the code doesn't work. Do I need to make any adjustment to the array because all the variables are character now ?

 

 

PaigeMiller
Diamond | Level 26

Try my other idea: "Change your array statement so it only has numeric vars. Run the code. Change the array statement so it only has character vars. Run the code again."

--
Paige Miller
Satori
Quartz | Level 8

That won't give me the same result. What I want is to iteratively add one variable based on a specific order. Character variables are in the middle of numeric variables and vice versa. 

Astounding
PROC Star

That can be done.  But we need to know what you are hoping to achieve.  For example:

  1. For each observation, find the name of the first variable that has a missing value.
  2. For each observation, find the name (in order) of all variables that have missing values.
  3. Something else?

Programming won't be lengthy, but may be complex depending on what is needed.

PaigeMiller
Diamond | Level 26

@Satori wrote:

That won't give me the same result. What I want is to iteratively add one variable based on a specific order. Character variables are in the middle of numeric variables and vice versa. 


You want to add numbers to characters? You can't do that. Please explain further.

 

Do you really want to "iteratively add one variable based on a specific order"? Or is this question about your earlier thread at https://communities.sas.com/t5/SAS-Programming/count-not-missing-for-multiple-variables-simultaneous... Is this really about your earlier thread, yes or no? 

 

If your earlier thread is what you want, please do not say you want to "iteratively add one variable based on a specific order", because that's not what you want. Your earlier thread wanted the number missing to be computed, iteratively, across many columns.

 

If your earlier thread is what you want, you should state that clearly at the beginning so we have context to help us understand, and then possibly we can find an answer that fits into that code, without starting from scratch. Do not keep us in the dark. Do not withhold context and explanation.

 

 

--
Paige Miller
ballardw
Super User

 

Maybe something like this:

data example;
  input v1 v2 v3 $ v4 $;
datalines;
. . . p
1 2 . d
1 . a q
1 4 b z
;

proc format;
value ms
low-high='Nonmissing'
other='Missing'
;
value $ms
' '='Missing'
other='Nonmissing'
;
proc freq data=example;
   ods output onewayfreqs=freqtable;
   tables v1-v4 /;
   format _numeric_ ms. _character_ $ms.;
run;

data want;
   set freqtable;
   length var $ 32.;
   var = scan(table,2);
   keep var frequency;
   label frequency='Nonmissing';
run;

 

The above attempts to "automagically" adjust for inconsistent variable descriptions by using the list variables _character_ and _numeric_. If you have variables other than your v1 to v50, or whatever the actual names may be, you would need to drop /keep the desired variables on the data=example (keep=<list of variables>) in proc freq so only the ones involved in this exercise are actually used. The ODS OUTPUT places the results of all the frequencies into a single table, which is somewhat odd in structure but since you only want to count nonmissing that is easy to deal with. The Formats mean there is only one nonmissing value reported for each variable so the table is very simple to clean up to extract the bits needed.

 

Note: Your original question should have provided data similar to what you work with, including character and numeric variables. When you do not explain the problem clearly at the beginning then you get incomplete solutions. There are many things that work only for numeric variables that when you throw in character variables will not work.

 

 

s_lassen
Meteorite | Level 14

One way to do it is this: generate the code for checking, and put it in a temporary file, which you can %include:

filename tempsas temp;                                                                                                                  
data _null_;                                                                                                                            
  if 0 then set have;                                                                                                                   
  file tempsas;                                                                                                                         
  do varnum=1 to 58;                                                                                                                    
    varname=cats('VAR',varnum);                                                                                                         
    if vtypex(varname)='N' then put                                                                                                     
      'if not ' varname 'then return;';                                                                                                 
    else put                                                                                                                            
      'if missing(' varname ') then return;';                                                                                           
    put 'nonmiss' varnum '+1;';                                                                                                         
    end;                                                                                                                                
  stop;                                                                                                                                 
run;                                                                                                                                    
                                                                                                                                        
data want;                                                                                                                              
  set have end=eof;                                                                                                                     
  retain nonmiss1-nonmiss58 0;                                                                                                          
  link checkvars;                                                                                                                       
  if eof;                                                                                                                               
  keep nonmiss:;                                                                                                                        
  return;                                                                                                                               
checkvars:                                                                                                                              
  %include tempsas;                                                                                                                     
return;                                                                                                                                 
run;

I am not sure what check you want on the character variables, you may want to change the line after "else put".

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
  • 892 views
  • 2 likes
  • 5 in conversation