14.03.2014 Views

Scripting Guide - SAS

Scripting Guide - SAS

Scripting Guide - SAS

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

170 Data Structures Chapter 7<br />

Associative Arrays<br />

Create an associative array from two column references. The first column contains the keys and the second<br />

contains the values.<br />

map = Associative Array(:name, :height);<br />

Create an associative array from two column references with a default value:<br />

map = Associative Array(:name, :height, .);<br />

Create an associative array from a single list of keys or a single column reference of keys with a default value<br />

of 0:<br />

set = Associative Array({"yes", "no"});<br />

set = Associative Array(:name);<br />

Work with Associative Arrays<br />

Find the Number of Keys<br />

To determine the number of keys that an associative array contains, use the N Items() function.<br />

cary = Associative Array();<br />

cary["state"] = "NC";<br />

cary["population"] = 116234;<br />

cary["weather"] = "cloudy";<br />

cary["population"] += 10;<br />

cary["weather"] = "sunny";<br />

cary["high schools"] = {"Cary", "Green Hope", "Panther Creek"};<br />

N Items(cary);<br />

4<br />

Add and Delete Keys and Values<br />

To add or delete key-value pairs from an associative array, use the following functions:<br />

• Insert()<br />

• Insert Into()<br />

• Remove()<br />

• Remove From()<br />

Note the following:<br />

• Insert() and Remove() return a named copy of the given associative array with the key-value pair<br />

added or removed.<br />

• Insert Into() and Remove From() add or remove the key-value pairs directly from the given<br />

associative array.<br />

• Insert() and Insert Into() take three arguments: the associative array, a key, and a value.<br />

• Remove() and Remove From() take two arguments: the associative array and a key.<br />

• If you insert a key with no value provided, the key is assigned a value of 1.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!