SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
New_to_SAS
Calcite | Level 5

Hi,

 

I have a simple array that I want to use, but I did not drop the (i) at the end of the array since I thought it will be the same number as the elements in the array. What I notice though is that it adds a 1 to the number of elements in the array. Do you know why that is happening, and is the a way I can have it reflect the number of the elements in the array?
 
Here is the array
 
Array demography [*] $ C_sex D_DOB D_AGE E_POPULATION;
do i=1 to dim(demography);
if demography(i) ne " " then count_demo+1;
end;
 
I would like i to reflect that there are 4 elements in this array. Is that posible, or should I continue to use a counter?
 
Thanks
 
1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

I think you just need to use the DIM function.

 

Array demography [*] $ C_sex D_DOB D_AGE E_POPULATION;
number_of_elements=dim(demography);

 

--
Paige Miller

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

I think you just need to use the DIM function.

 

Array demography [*] $ C_sex D_DOB D_AGE E_POPULATION;
number_of_elements=dim(demography);

 

--
Paige Miller
RW9
Diamond | Level 26 RW9
Diamond | Level 26

i has nothing to do with the array.  i is an incrementor.  The process for the do loop: do i=1 to 4;

Is;

set i to 1

Is i > end of loop

No - run code inside

set i to i + 1

Is i > end of loop 

No - run code inside

 

As you can see each time the loop enacts i is incremented, the loop does not run the code enclosed with the loop block when i > 4, however i still gets incremented by 1, as otherwise i would never be greater than the end of loop.  What is it your actually trying to achieve (post full example) as:

data have;
  set sashelp.class;
  if _n_=4 then weight=.;
  if _n_=8 then sex="";
run;

data want;
  set have;
  result=nmiss(age,weight,height) + cmiss(sex);
run;

Is a simpler solution.

New_to_SAS
Calcite | Level 5

"number_of_elements=dim(demography); " worked. Thank you for the responses.

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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
  • 3 replies
  • 886 views
  • 2 likes
  • 3 in conversation