BookmarkSubscribeRSS Feed
NAO_JUL_13
Fluorite | Level 6

お世話になっております。

 

SASプログラム内で、ハッシュテーブルを使用する際の方法について質問です。

 

下記のようなハッシュテーブルマクロを用いる際、SAS名やテーブル名に

「TABLENAME('%')」や「COLNAME('+')」といった特殊文字を含める場合、

どのように指定すればよろしいでしょうか?

 

============================

declare hash hwork(dataset: 'TABLENAME' );
  hwork.definekey('COLNAME');
  hwork.definedata('COLNAME');
  hwork.definedone();

============================

 

何卒よろしくお願いいたします。

7 REPLIES 7
MichelleHomes
Meteorite | Level 14

You might find this tip sheet with examples helpful https://support.sas.com/rnd/base/datastep/dot/hash-tip-sheet.pdf

 

Kind Regards,

Michelle

//Contact me to learn how Metacoda software can help keep your SAS platform secure - https://www.metacoda.com
NAO_JUL_13
Fluorite | Level 6

Hello, Michelle.

Thanks for your reply,  but  unfortunately that can not solve my question.

 

For example, I attached sample program and log-file, could you check them?

 

In this case, 1st data step has made the dataset "name('%')" correctly.

But the next data step has been failed at L.15 and L.16 .

 

** I tried to use %bquote and %superq  functions, but they confused me ...

 

I really hope your kindest helps.

regards.

Kurt_Bremser
Super User

You can avoid all that pain in a very simple way by using standard SAS names (letters, digits, underlines) and avoiding the 'some crazy stuff'n construct. Put fancy descriptions and characters in labels, where they belong. This will make your future SAS life much easier.

MichelleHomes
Meteorite | Level 14

Hi,

 

As Kurt mentions it might be best to avoid using the special characters such as % in your variable names. Particularly as these are special tokens that SAS parses at compile time unless they are escaped appropriately. I'm not sure whether they can be escaped in the hash object definitions. There seems to be some rules to be aware of when working with hash objects and that % is a special character - see the last point at http://support.sas.com/documentation/cdl/en/lecompobjref/63327/HTML/default/viewer.htm#n1f2an4fc14u4...

 

FYI, one of my favourite SAS documentation sections on how SAS processes a program and special tokens which might be helpful is http://support.sas.com/documentation/cdl/en/mcrolref/62978/HTML/default/viewer.htm#p1lpbl12k9hhkxn1m...

 

Kind Regards,

Michelle

//Contact me to learn how Metacoda software can help keep your SAS platform secure - https://www.metacoda.com
NAO_JUL_13
Fluorite | Level 6

Fine, and thanks a lot for you helps.

 

We would like to guide using simple SAS names for our users.

 

Thank you for your kindest helps, regards.

MichelleHomes
Meteorite | Level 14

どういたしまして

 

Kind Regards,

Michelle

//Contact me to learn how Metacoda software can help keep your SAS platform secure - https://www.metacoda.com
mokuson
Calcite | Level 5

9.4M5での動作確認結果です。

マニュアルには仕様が記載されていないので別バージョンで動くのかわかりません。

%よりも括弧のほうが問題っぽいです。

 

データセット名に括弧が無ければ次で動く

データセット名:NAMES'%'

PG抜粋:

declare hash ht(dataset:"""names'%'""n");
ht.defineKey('first(''%'')', "last");
ht.defineData("born", "died", "title");
ht.defineDone();

 

データセット名に括弧があるとdeclare hash htは初めに出てきた括弧を

データセットオプションの開始だと思ってしまうので

上記の書き方だとダメ。(ちなみに一つ目の括弧-括弧閉じの後続文字列は無視されるっぽい)

 

マニュアルには記載されていないですが、

declare hash htの1バイト目から括弧だとsyslastマクロ変数に設定されている

データセットから読み込むっぽい。さらに特殊文字大丈夫

データセット名:names('%')

PG抜粋:

%let syslast = WORK."NAMES%str(%(%'%%%'%))"n;
/* Load and Find */
data _null_;
length 'first(''%'')'n last title $ 16;
length born died 8;
declare hash ht(dataset:"(where=(1=1))");

ht.defineKey("first('%')", "last");
ht.defineData("born", "died", "title");
ht.defineDone();

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

Discussion stats
  • 7 replies
  • 1946 views
  • 7 likes
  • 4 in conversation