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.
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.
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;
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.
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.
Please supply some test data to demonstrate that effect.
You should also recheck your code. You reference array maingroup in the do block, but declare array maingroups.
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.
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.
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.
Then place $ after maingroups and you can size it with a numeric if it is more than 8
array maingroups{14} $;
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!
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.