Hi, I have borrowed this code which works ok and I don't understand the hash object just yet. I would like to know if there is a way to have the variable 'sourcedID' as the directory name instead of part of the filename.
For example it currently creates
E:\Test\ orgs_350.sas7bdat
E:\Test\orgs_360.sas7bdat
etc.
and I would like this:
E:\Test\350\orgs.sas7bdat
E:\Test\360\orgs.sas7bdat
Thanks for any help!
libname e 'E:\Test';
data _null_;
if _n_=1 then do;
if 0 then set work.orgs;
declare hash h(dataset:'work.orgs(obs=0)',multidata:'y');
h.definekey(all:'y');
h.definedata(all:'y');
h.definedone();
end;
do until(last.sourcedid);
set work.orgs;
by sourcedId;
h.add();
end;
h.output(dataset:"e.orgs_" || sourcedid );
h.clear();
run;
Unlike normal SAS code that allows you use to a quoted physical name to reference a dataset for the OUTPUT() method of the HASH object have to use the old standard MEMNAME or LIBNAME.MEMNAME format for the dataset name.
But you can use the LIBNAME() function to dynamically define the libref before calling the OUTPUT() method.
Also if you set the DLCREATEDIR option then making the libref will create the subfolder if it does not exists (note that DLCREATEDIR will only create the terminal subdirectory, the higher levels have to already exist.).
rc=libname('E',cats("E:\test\",sourcedid));
h.output(dataset:"e.orgs");
Hi @kgflynn The Output method creates a dataset or in other words write the contents of the Hash object to a dataset. The e. expression in the output method basically prefixes the dataset name to two level reference name pointing to a library of a folder. Removing the prefix e. would still create the dataset but in the work library.
So basically the functionality of Hash object output method is not to create libraries, rather create only datasets if that makes sense.
You could write the expression like
(dataset:cats("e.", "orgs_" , sourcedid ); and still means the same.
However, having said the above, you could probably use DCREATE function and create a directory when processing each By group dynamically and point the expression of the DCREATE function in the HASH object Output method. This should be possible.
Unlike normal SAS code that allows you use to a quoted physical name to reference a dataset for the OUTPUT() method of the HASH object have to use the old standard MEMNAME or LIBNAME.MEMNAME format for the dataset name.
But you can use the LIBNAME() function to dynamically define the libref before calling the OUTPUT() method.
Also if you set the DLCREATEDIR option then making the libref will create the subfolder if it does not exists (note that DLCREATEDIR will only create the terminal subdirectory, the higher levels have to already exist.).
rc=libname('E',cats("E:\test\",sourcedid));
h.output(dataset:"e.orgs");
Thanks for the quick responses. Just what I needed!
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.
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.