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

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;

1 ACCEPTED SOLUTION

Accepted Solutions
FriedEgg
SAS Employee

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

6 REPLIES 6
art297
Opal | Level 21

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).

FriedEgg
SAS Employee

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.

DLing
Obsidian | Level 7

**** 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.

data_null__
Jade | Level 19

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

HyunJee
Fluorite | Level 6

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!

HyunJee
Fluorite | Level 6

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 6 replies
  • 7109 views
  • 6 likes
  • 5 in conversation