BookmarkSubscribeRSS Feed
mark4
Obsidian | Level 7

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

SASKiwi
PROC Star

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.   

Tom
Super User Tom
Super User

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

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 17 replies
  • 3830 views
  • 3 likes
  • 7 in conversation