Thanks. That's too bad you can't access hashes outside data steps. It's such an inelegant solution. Yet another reason having to segment SAS programs into all sorts of proc's, data sets, macros is so cumbersome...
I think you need to stop comparing SAS programs with the way other languages have implemented stuff - every language has its idiosyncracies. There are good reasons why SAS is structured the way it is and as your knowledge grows you will see many areas where SAS goes way beyond what you can do in other languages. To give you an example, using SAS formats as lookups is incredibly powerful. I've built DATA steps that do in excess of 100 SAS format lookups - try doing that in SQL table joins or hash lookups.
@mark4 wrote:
Does SAS have something similar to Python's dictionary? For those unfamiliar, an easy way to think about it is a data set with two columns - name and value. For example, the 'name' column could be a person's name, and the 'value' column could be their address. One great feature about this is the ability to quickly 'look-up' the value for a given name. If the dictionary was called dict, then to get John Doe's address, you evaluate the expression dict['John Doe'].
...
Yes.
They are called formats. Let's take your example of translating a name to an address. For that you would create a character format (since name is character value).
proc format ;
value $address
'John Doe'='101 Main Street'
;
run;
You could then use the format to display the addresses. For example to get frequency counts by address you could just do:
proc freq data=users ;
tables name ;
format name $address.;
run;
If you want to create a new variable that has the formatted value then use the PUT function.
data new ;
set old;
address=put(name,$address.);
run;
If you already have the data for your format in a dataset then you can either use the data to generate the VALUE statement of PROC FORMAT.
filename code temp;
data _null_;
set name_address_list end=eof;
file code ;
if _n_=1 then put 'value $address';
put name :$quote. '=' address :$quote. ;
if eof then put ';';
run;
proc format;
%include code;
run;
Or convert the data into the data format that PROC FORMAT can use for generating formats.
data fmtin ;
set name_address_list (keep=name address) ;
fmtname='$address';
rename name=start address=label;
run;
proc format cntlin=fmtin;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.