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

I have a question related to variable scope.  The example snippet may go this way:

 

------------------------------

proc ds2;

   data _null_;

       /*THIS IS A REGULAR VAR & AN ARRAY*/

      dcl int somenum;

      dcl int numarray[3];

 

     /*ASSIGNING VALUES*/ 

    method init();

        somenum=100;

       numarray := (10, 11, 12);

    end;

 

    /*CHECKING */

   method run();

        set sashelp.class;

       put somenum= numarray[1]= numarray[2]= numarray[3]=;

  end;

 

enddata;

run;

quit;

 

----------------------

The result would show that the values in numarray are retained but not the variable somenum.  Why?

 

Thanks for helping out,

 

Jason

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
SASJedi
SAS Super FREQ

In your code, somenum is a globally declared variable. Global scope variables reside in the Program Data Vector (PDV), are available within all methods in the data program, and are eligible to be included in the data program result set (if not excluded with a DROP or KEEP statement). Because the somenum was created by the data program and not read in from a SET statement, the PDV mechanism re-initializes this value at the beginning of every iteration of the RUN() method.

 

However, numarray is a globally declared array. Global scope arrays are available to all of the methods in the data program and are eligible to be included in the data program result set - but they are NOT associated with PDV variables. The elements of a multi-dimensional data structure created with a DECLARE (DCL) statement reside in system memory.  Because the elements do not reside in the PVD, they are never initialized by the PDV mechanism, and cannot be included in the data program result set,

 

To create an array of PDV variables, use the VARARRAY statement.

 

I hope this helps

Mark

 

Check out my Jedi SAS Tricks for SAS Users

View solution in original post

3 REPLIES 3
SASJedi
SAS Super FREQ

In your code, somenum is a globally declared variable. Global scope variables reside in the Program Data Vector (PDV), are available within all methods in the data program, and are eligible to be included in the data program result set (if not excluded with a DROP or KEEP statement). Because the somenum was created by the data program and not read in from a SET statement, the PDV mechanism re-initializes this value at the beginning of every iteration of the RUN() method.

 

However, numarray is a globally declared array. Global scope arrays are available to all of the methods in the data program and are eligible to be included in the data program result set - but they are NOT associated with PDV variables. The elements of a multi-dimensional data structure created with a DECLARE (DCL) statement reside in system memory.  Because the elements do not reside in the PVD, they are never initialized by the PDV mechanism, and cannot be included in the data program result set,

 

To create an array of PDV variables, use the VARARRAY statement.

 

I hope this helps

Mark

 

Check out my Jedi SAS Tricks for SAS Users
jason4sas
Obsidian | Level 7

It's a great explanation.  Thanks Mark.  

 

I kind of like the properties of the temporary array in DS2, due to its global scope, persistence during iteration, and being auto dropped in the end.  Is there any other identifier like it in DS2?  Also, is there any downside with it, such as processing speed etc.?  Thanks.

SASJedi
SAS Super FREQ

Arrays are one of the look-up table methods available in SAS. They are memory-resident, so performance is pretty snappy. The index is strictly numerical, so not as flexible as a hash object which can use numeric, character or even composite keys. Hashes can also return multiple data values for each lookup key. However, the hash object uses PDV-resident variables to specify key values and retrieve data. Threads in DS2 run "shared nothing", so when arrays and hash objects are specified in thread programs, at execution each thread spawned gets its own copy. This can become quite memory intensive.

 

User-defined formats are another way of performing lookups in SAS (including DS2), and are also pretty fast and flexible. They don't contaminate the PDV, don't need to be duplicated when threading, and are re-usable in subsequent program steps.

May the SAS be with you!

Mark

Check out my Jedi SAS Tricks for SAS Users

 

This is a knowledge-sharing community for learners in the Academy. Find answers to your questions or post here for a reply.
To ensure your success, use these getting-started resources:

Estimating Your Study Time
Reserving Software Lab Time
Most Commonly Asked Questions
Troubleshooting Your SAS-Hadoop Training Environment

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
  • 3 replies
  • 1269 views
  • 2 likes
  • 2 in conversation