DATA Step, Macro, Functions and more

Understanding code for using do loops to populate 2-dimensional array from stored array values

Reply
New User
Posts: 1

Understanding code for using do loops to populate 2-dimensional array from stored array values

I am currently studying for my Advanced SAS certification using Chapter 16 of the Advanced Prep Guide.  On page 586 is SAS code for using nested do-loops to load an 2-dimensional array ("Targets") from a SAS dataset (sasuser.ctargets) which contains stored array data.  The beginning of the code reads as follows:

data work.lookup;

  array Target{1997:1999, 12} _temporary_;

  if _n_=1 then do i = 1 to 3;
     set sasuser.ctargets;

     array mnth(*) Jan--Dec;

     do j=1 to dim(mnth);

        targets(year,j) = mnth(j);

     end;

  end;

[more code ... that transfers the data from the targets array to the data set work.lookup]

My question concerns where the data from sasuser.ctargets lives when it is "set," and how the 3 iterations of the outer do-loop moves down the data coming from sasuser.ctargets to transfer it into the arrany mnth.

I read the "set sasuser.ctargets" statement as reading the data from sasuser.ctargets into the data set work.lookup -- but work.lookup is eventually used to build the new data set that only contains data from sasuser.ctargets in a sort of a transposed format.  I also do not see where "year" gets defined -- it just appears when needed to indicate the row number.  Since year is not defined, and since year does not = i, how does the outer do loop know to iterate down the rows?

 

 

Spoiler
Spoiler
sas

 

Super User
Super User
Posts: 7,955

Re: Understanding code for using do loops to populate 2-dimensional array from stored array values

[ Edited ]

I will not answer your question directly as its better to work these things out yourself.  I will however give a small pointer.  If you want to know what is happening at any point in a datastep, you can use the syntax:

    put _all_;

Or in fact any text or variable you want, so if you want some of of the variables and some text:

put "Some text:" var1;

 

You will also want to read the documentation on the Program Data Vector which is where the data is placed and manipulated on each iteration.

http://support.sas.com/resources/papers/proceedings12/255-2012.pdf

http://support.sas.com/documentation/cdl/en/basess/58133/HTML/default/viewer.htm#a001290590.htm

 

I assume ctarget is a UE dataset as I dont have that one.

Super User
Posts: 19,802

Re: Understanding code for using do loops to populate 2-dimensional array from stored array values

This demonstrates a lookup using temporary arrays, where the temporary array is loaded from a dataset. 

 

YEAR and Jan-Dec are variables in the dataset sasuser.ctargets.

The i=1 to 3, controls the loop over the rows, and the j=1 to 12 controls moving across the columns.

 

 

 

 

 

 

 

Super User
Posts: 11,343

Re: Understanding code for using do loops to populate 2-dimensional array from stored array values

What does Proc contents on sasuser.ctargets show?
Super User
Posts: 19,802

Re: Understanding code for using do loops to populate 2-dimensional array from stored array values

The data is here:

http://support.sas.com/publishing/cert/sampdata.txt

 

data sasuser.ctargets;
   attrib Year length=8;
   attrib Jan length=8;
   attrib Feb length=8;
   attrib Mar length=8;
   attrib Apr length=8;
   attrib May length=8;
   attrib Jun length=8;
   attrib Jul length=8;
   attrib Aug length=8;
   attrib Sep length=8;
   attrib Oct length=8;
   attrib Nov length=8;
   attrib Dec length=8;

   infile datalines dsd;
   input
      Year
      Jan
      Feb
      Mar
      Apr
      May
      Jun
      Jul
      Aug
      Sep
      Oct
      Nov
      Dec
   ;
datalines4;
1997,192284420,86376721,28526103,260386468,109975326,102833104,196728648,236996122,112413744,125401565,72551855,136042505
1998,108645734,147656369,202158055,41160707,264294440,267135485,208694865,83456868,286846554,275721406,230488351,24901752
1999,85730444,74168740,39955768,312654811,318149340,187270927,123394421,34273985,151565752,141528519,178043261,181668256
;;;;
run;
Super User
Posts: 11,343

Re: Understanding code for using do loops to populate 2-dimensional array from stored array values


Bruce1 wrote:

  I also do not see where "year" gets defined -- it just appears when needed to indicate the row number.  Since year is not defined, and since year does not = i, how does the outer do loop know to iterate down the rows?

 

 

Spoiler
Spoiler
sas

 


Year is obviously in the ctargets dataset. Any numeric variable (hopefully within range and integer) can be used to reference elements in the Array. That is why the Array definition had that {1997:1999, 12} to say there were three rows and the valid row indicators had values of 1997, 1998 and 1999. Which happen to be years in the data.

Ask a Question
Discussion stats
  • 5 replies
  • 253 views
  • 0 likes
  • 4 in conversation