DATA Step, Macro, Functions and more

Why does SAS think an array element/variable is neither missing nor nonmissing?

Accepted Solution Solved
Reply
Contributor
Posts: 21
Accepted Solution

Why does SAS think an array element/variable is neither missing nor nonmissing?

[ Edited ]

Hello SAS Community:

 

i have a data step with a series of arrays and loops...a simplified version below:

 

data out;
set in;
%let d=500;
array codes{&d}; *codes1-codes500 already exist in "n";
array dates{&d}; *same as above;
array maingroup{14}; *we create array and use like variables;

do i=1 to &d;
 if year(dates{i}) eq yearbefore then do;
   currgroup=substr(codes{i},1,1);
   if missing(maingroup{1}) then maingroup{1}=currgroup;
end;
end;
run;

the "maingroup1" var from the declared array never sets properly. Even when i put "if not missing(maingroup{1}) it doesn't set it. So whether or not i say maingroup{1} (or maingroup1 for that matter) is missing or not missing, it won't set the variable.

 

Can anyone explain what state this array element/variable has if it is neither missing nor not missing? 


Thanks ahead of time.


Accepted Solutions
Solution
‎09-14-2016 08:46 AM
Super User
Posts: 5,099

Re: Why does SAS think an array element/variable is neither missing nor nonmissing?

There are several red flags that may be nothing, may be carelessness, or may be a real issue.

 

Are you looking at maingroup14?  There is nothing in your program that would assign a value to maingroup14.  The is a hard-coded reference to maingroup1.

 

The name of the array is maingroups, not maingroup.  So the elements are maingroups1-maingroups14.

 

The earlier array elements must be correct.  For example, the elements of the DATES array must be legitimate dates (not datetimes).  And the elements of the CODES array must be character (numeric would cause the results you are describing), and must be left-hand justified.

 

 

 

 

View solution in original post


All Replies
Super User
Posts: 6,972

Re: Why does SAS think an array element/variable is neither missing nor nonmissing?

[ Edited ]

You need to tell SAS which variables from the PDV belong to an array, otherwise the array is created with new variables and always set to missing at the start of a data step iteration.

Do it like this

array codes{&d} atc1-atc&d;

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 21

Re: Why does SAS think an array element/variable is neither missing nor nonmissing?

Hi Kurt,

Thank you for responding but this doesn't answer the question. The codes arrays are fine (when the variables exist matching array declared they stick just fine), the problem seems to be with the maingroup{14} which does not set properly and has neither missing nor not missing state.
Super User
Posts: 6,972

Re: Why does SAS think an array element/variable is neither missing nor nonmissing?

If array dates is not assigned to proper variables, this condition

if year(dates{i}) eq yearbefore then do;

can never be true and the do block will never execute.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 21

Re: Why does SAS think an array element/variable is neither missing nor nonmissing?

That isn't true, the do block executes just fine, because the dates array exist in variables, and the currgroup variable sets just fine. The problem (once again) is that the maingroup arrays does not create seem to make the maingroup1-maingroup14 variables, because they do not have a state of missing or nonmissing.

Super User
Posts: 6,972

Re: Why does SAS think an array element/variable is neither missing nor nonmissing?

Please supply some test data to demonstrate that effect.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 6,972

Re: Why does SAS think an array element/variable is neither missing nor nonmissing?

You should also recheck your code. You reference array maingroup in the do block, but declare array maingroups.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Solution
‎09-14-2016 08:46 AM
Super User
Posts: 5,099

Re: Why does SAS think an array element/variable is neither missing nor nonmissing?

There are several red flags that may be nothing, may be carelessness, or may be a real issue.

 

Are you looking at maingroup14?  There is nothing in your program that would assign a value to maingroup14.  The is a hard-coded reference to maingroup1.

 

The name of the array is maingroups, not maingroup.  So the elements are maingroups1-maingroups14.

 

The earlier array elements must be correct.  For example, the elements of the DATES array must be legitimate dates (not datetimes).  And the elements of the CODES array must be character (numeric would cause the results you are describing), and must be left-hand justified.

 

 

 

 

Contributor
Posts: 21

Re: Why does SAS think an array element/variable is neither missing nor nonmissing?

This did it -- the maingroup array i just had to declare as a character array:

array maingroup{14} $;

and now it is behaving in setting the code (string)
Super User
Posts: 6,972

Re: Why does SAS think an array element/variable is neither missing nor nonmissing?

When you look at the log, you will see NOTEs about conversion of numeric values to character values. These should ALWAYS raise a red flag for you, as they may cause bootloads of unexpected problems (as you just experienced).

Good code never has those NOTEs.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 21

Re: Why does SAS think an array element/variable is neither missing nor nonmissing?

Thanks Kurt you're right, the NOTEs i do not take as seriously as I should. I appreciate your time and assistance this afternoon.
Super Contributor
Posts: 257

Re: Why does SAS think an array element/variable is neither missing nor nonmissing?

You declared an array like:

 

array maingroups{14}; *we create array and use like variables;

and you are accessing it as:

 

  if missing(maingroup{1}) then maingroup{1}=currgroup;

 

The two are different for SAS.

Contributor
Posts: 21

Re: Why does SAS think an array element/variable is neither missing nor nonmissing?

Hi datasp,

Sorry this was me just typo'ing when getting the code typed into forum... issue was character format of codes arrays which was not setting numeric maingroup array properly..
Super Contributor
Posts: 257

Re: Why does SAS think an array element/variable is neither missing nor nonmissing?

Then place $ after maingroups and you can size it with a numeric if it is more than 8

array maingroups{14} $;

 

☑ This topic is solved.

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

Discussion stats
  • 13 replies
  • 504 views
  • 3 likes
  • 4 in conversation