BookmarkSubscribeRSS Feed
emaguin
Quartz | Level 8

Ron Cody shows this bit of code on his blog and Tom has a similar example in another reply.

data New;
set Original;
array Nums[*] _numeric_;
array Chars[*] _character_;
do i = 1 to dim(Nums);
if Nums[i] = 999 then Nums[i] = .;
end;

do i = 1 to dim(Chars);
Chars[i] = upcase(Chars[i]);
end;
drop i;
run;
My specifics are a little simpler.

* recode '.', '.p', '.s' to -99 for all variables;
data par_teen1; set par_teen;
array md{*} _numeric_;
do i=1 to dim{md};
if (md{i} eq .p) then md{i}=-99;
end;
run;

This is the result.

417 * recode '.', '.p', '.s' to -99 for all variables;
418 data par_teen1; set par_teen;
419 array md{*} _numeric_;
420 do i=1 to dim{md};
ERROR: Undeclared array referenced: dim.
ERROR: Illegal reference to the array md.
ERROR: Variable dim has not been declared as an array.
421 if (md{i} eq .p) then md{i}=-99;
422 end;
423 run;

I've verified by inspection that the type of all vars (~170) are Num.

I'm doing something wrong; tell me what it is.

Thank you.

4 REPLIES 4
ballardw
Super User

DIM is the function to return the defined size of an array. As other functions you must use the simple parentheses ( ) and not the brackets [ ] or braces { } around the argument of the function.

 

 

 

Instead of

 if (md{i} eq .p) then md{i}=-99;

since you have multiple values assigned to the same recode:

 if (md{i} in (. .p .s) ) then md{i}=-99;

I also hope this recode is only done for reporting or export purposes. Otherwise any statistic or calculations done with these variables will be terribly suspect in the result.

Tom
Super User Tom
Super User

@ballardw wrote:

...

The Array statement also requires ( ) not { }. The brackets and braces in a data step are reserved for already defined arrays and character values, something appearing inside quote marks.

...


That statement is false. 

You can use any of (), {} or [] when setting the dimensions of an array.

 

array md (*) _numeric_;
array md [*] _numeric_;
array md {*} _numeric_;

 

In fact the array statement in a DATA step does not need to have anything at all at that point when you have provided it a list of variable names, like in this case.

array md _numeric_;

But there are some newer places where SAS allows the use of an ARRAY statement such as PROCs that included code blocks and PROC FCMP. And some of those do required the dimension setting to be included, even when they allow the use * instead of an actual dimension.  

quickbluefish
Quartz | Level 8
dim is a function - you have to use actual parentheses with it.
Tom
Super User Tom
Super User

When debugging a SAS program you need fix the FIRST error you get. 

ERROR: Undeclared array referenced: dim.

Because once you have one error the following errors might just be more symptoms of that first mistake. 

 

This is actually what is happening with your code.  The error is the attempt to reference an array named DIM that you did not define.  If you fix that then the other errors disappear also.

1    data test;
2      set sashelp.class;
3      array md{*} _numeric_;
4      do i=1 to dim{md};
ERROR: Undeclared array referenced: dim.
ERROR: Illegal reference to the array md.
ERROR: Variable dim has not been declared as an array.
5        if (md{i} eq .p) then md{i}=-99;
6      end;
7    run;

NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.TEST may be incomplete.  When this step was stopped there were 0 observations and 6 variables.
WARNING: Data set WORK.TEST was not replaced because this step was stopped.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds

If you change the reference to the undefined DIM array to a call to the DIM() function then the error message about the MD array goes away also.

8    data test;
9      set sashelp.class;
10     array md{*} _numeric_;
11     do i=1 to dim(md);
12       if (md{i} eq .p) then md{i}=-99;
13     end;
14   run;

NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.TEST has 19 observations and 6 variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds

 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 195 views
  • 5 likes
  • 4 in conversation