BookmarkSubscribeRSS Feed
Bruce1
Calcite | Level 5

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

 

5 REPLIES 5
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Reeza
Super User

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.

 

 

 

 

 

 

 

ballardw
Super User
What does Proc contents on sasuser.ctargets show?
Reeza
Super User

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;
ballardw
Super User

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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
  • 5 replies
  • 893 views
  • 0 likes
  • 4 in conversation