🔒 This topic is solved and locked.
Need further help from the community? Please
sign in and ask a new question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Posted 08-20-2018 09:31 AM
(885 views)
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;
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
Paige Miller
3 REPLIES 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
"number_of_elements=dim(demography); " worked. Thank you for the responses.