BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
kgflynn
Fluorite | Level 6

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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");

View solution in original post

3 REPLIES 3
novinosrin
Tourmaline | Level 20

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.

Tom
Super User Tom
Super User

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");
kgflynn
Fluorite | Level 6

Thanks for the quick responses.  Just what I needed!

sas-innovate-white.png

Missed SAS Innovate in Orlando?

Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1683 views
  • 6 likes
  • 3 in conversation