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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

13 REPLIES 13
Kurt_Bremser
Super User

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;

 

chrisengel
Obsidian | Level 7
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.
chrisengel
Obsidian | Level 7

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.

Astounding
PROC Star

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.

 

 

 

 

chrisengel
Obsidian | Level 7
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)
Kurt_Bremser
Super User

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.

chrisengel
Obsidian | Level 7
Thanks Kurt you're right, the NOTEs i do not take as seriously as I should. I appreciate your time and assistance this afternoon.
KachiM
Rhodochrosite | Level 12

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.

chrisengel
Obsidian | Level 7
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..
KachiM
Rhodochrosite | Level 12

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

array maingroups{14} $;

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 13 replies
  • 1697 views
  • 3 likes
  • 4 in conversation