/***************************************************************************************************************** SAS file name: puthash.sas File location: __________________________________________________________________________________________________________________ Purpose: To demonstrate how to print the content of a SAS hash object in the log with the PutHash macro. Author: Peter Clemmensen Creation Date: 23/05/2020 This program supports the blog post "Print Content of a SAS Hash Object in the Log" on SASnrd.com *****************************************************************************************************************/ /* Richard A. Devenezia's macro */ %macro putHash (hash, vars); %* %* hash - variable that references a hash object %* vars - space separated list of variables linked to the data items of hash %* separate with pound sign (#) to get varname=varvalue format %*; %* generate a random variable name; %local random hi rc; %let random = %substr(%sysfunc(ranuni(0),10.8),3); %let hi = hi_&random; %let rc = rc_&random; %* emit DATA Step code that iterates the hash and %* puts the data items values in the log; declare hiter &hi ("&hash"); do &rc = &hi..first() by 0 while (&rc = 0); put %sysfunc(translate(&vars,=,#)); &rc = &hi..next(); end; &hi..delete(); put; %put WARNING: Values of variables &vars will change; %mend; /* Test it */ data _null_; declare hash h (dataset : 'sashelp.class'); h.definekey ('name'); h.definedata (all : 'Y'); h.definedone (); if 0 then set sashelp.class; %putHash (H,name#age#weight#sex#); run; data _null_; declare hash h (); h.definekey ('k'); h.definedata ('d'); h.definedone (); do k = 1, 2, 3; d = k*100; h.add(); %putHash (H, d#); end; run;