Help using Base SAS procedures

Dim function - need help understanding

Accepted Solution Solved
Reply
Contributor
Posts: 71
Accepted Solution

Dim function - need help understanding

Hello everyone. I started a new position and am going through the previous analysts code. I am not all too familiar with arrays and am trying to figure out a certain part of the script she left.

The areas I have a question on are followed by ************************** and an A or a B to distinguish which one I am asking a question on.

**********A

%let var = : this creates a marcro variable titled var that contains all the independent variables listed after the %let var = statement

*********B

From what I have been reading, the dim function always returns a total count of the number of elements in an array dimension. I cannot seem to understand why the variable created "x" contains the number 13 when there are

only 12 variables listed after %let var = function. Any help that can be given is much appreciated. Thank you.

HyunJee

LIBNAME PUF "C:\Users\jhale3\Desktop\NIS";

OPTIONS FMTSEARCH = (PUF WORK LIBRARY);;

%let in_file=puf.nisteenpuf09; *--- NAME OF SAS DATASET ---*;

%let estiap=estiapt09; * --- ESTIMATION AREA VARIABLE TO USE ---*;

%let wt=provwt; * --- WEIGHT TO USE ---*;

%let var = HPVI_RECOM IMM_ANY P_NUMHPV INCPOV1 Raceethk race_k educ1 num_provr facility registry  asthma P_UTDHPV;      *******************A

data sas_file;

set puf.nisteenpuf09 (keep= SEQNUMT HPVI_RECOM IMM_ANY P_NUMHPV INCPOV1  Raceethk race_k educ1 num_provr facility registry vfc_order pdat

            provwt estiapt09 state P_UTDHPV sex);

            /*array statement is counting the number of elements in a one-dimensional array of the number of elements in a specified dimension;*/

array newvar (*) &var;

do x = 1 to Dim(newvar);     *********************B

      if newvar(x) in (77 99) then newvar(x) = .;

end;

run;


Accepted Solutions
Solution
‎09-12-2011 03:41 PM
Trusted Advisor
Posts: 1,300

Re: Dim function - need help understanding

The dim function gives you the number of variables (dimensions) present in a given array.

These are the relevant lines in your code:

%let var = HPVI_RECOM IMM_ANY P_NUMHPV INCPOV1 Raceethk race_k educ1 num_provr facility registry  asthma P_UTDHPV;

array newvar (*) &var;

do x = 1 to Dim(newvar);

The array statement decalres your newvar array containing a number of dimensions (*) specified by a list of variables (&var).  &var according to your %let statement contains 12 variables so dim(newvar)=12 and your do loop with iterate from 1 to 12.

The do loop acts as follows:

start i=1; do stuff; i+1; check condition 1<=i<=12 (true); do stuff; i+1; check..... so ultimately i=13 and the check condition is false and the loop terminates.

View solution in original post


All Replies
PROC Star
Posts: 7,356

Dim function - need help understanding

Your understanding is correct but, in this case, dim(newvar) is equal to 12.  Could it be that you are confused because the value of x, by the time it leaves the loop, is 13?  If so, that is normal, the way the do loop is stated. The loop continues until the value of x is greater than the value of dim(newvar).

Solution
‎09-12-2011 03:41 PM
Trusted Advisor
Posts: 1,300

Re: Dim function - need help understanding

The dim function gives you the number of variables (dimensions) present in a given array.

These are the relevant lines in your code:

%let var = HPVI_RECOM IMM_ANY P_NUMHPV INCPOV1 Raceethk race_k educ1 num_provr facility registry  asthma P_UTDHPV;

array newvar (*) &var;

do x = 1 to Dim(newvar);

The array statement decalres your newvar array containing a number of dimensions (*) specified by a list of variables (&var).  &var according to your %let statement contains 12 variables so dim(newvar)=12 and your do loop with iterate from 1 to 12.

The do loop acts as follows:

start i=1; do stuff; i+1; check condition 1<=i<=12 (true); do stuff; i+1; check..... so ultimately i=13 and the check condition is false and the loop terminates.

Frequent Contributor
Posts: 104

Dim function - need help understanding

**** A     Correct.

**** B     There may be nothing wrong with the code at all.  The do loop is from 1 to 12, and x would contain the value of 13 when it exits the do loop.  That's how do loops stop - at one past the limit in your case.  If there was genuine out-of-bound indices, SAS LOG would contain error messages.

Respected Advisor
Posts: 3,777

Dim function - need help understanding

Sounds like in addition to unfamiliarity with arrays you are also unfamiliar with DO.  For a really nice treatment of the subject see.

http://analytics.ncsu.edu/sesug/2002/TU05.pdf

Contributor
Posts: 71

Dim function - need help understanding

this is a create resource. It is has been quite sometime since I have dealt with arrays and DO loops. this was just what I was needing!

Contributor
Posts: 71

Dim function - need help understanding

Thank you everyone for your help and advice in regard to my question. This is exactly what I needed to know. Thank you again!

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 461 views
  • 6 likes
  • 5 in conversation