-2

I'd like to know which characters are safe for any use in SAS macros.

So what I mean by special characters here is any character (or group of characters) that can have a specific role in SAS in any context. I'm not that interested in keywords (made of a-z 1-9 chars).

For example = ^= ; % , @ are special (not sure if @ is actually used in SAS, but it's used for doc so still count as a parameter that is not 'safe for all uses').

But what about $ ! ~ § { } ° etc ?

This should include characters that are special in PROC SQL as well.

I'd like to use some of these characters and give them a special meaning in my code, but I'd rather not conflict with any existing use (I'm especially interested in ~).

moodymudskipper
  • 46,417
  • 11
  • 121
  • 167
  • Not sure what you mean here. Any character (within the limits of the encoding your sessions is using) can be used as data. – Tom Apr 12 '18 at 13:53
  • They can be, but for example if I have a parameter `param1= a=b`, I'll run into trouble if I execute a macro function `%fun(param1)` without eliciting the parameter name explicitly (unless the parameter name is `a` and I know what I'm doing). – moodymudskipper Apr 12 '18 at 13:56
  • Sounds like you are asking about macro processing, which is really a different language than SAS. – Tom Apr 12 '18 at 13:57
  • So `=` would not be a character I'd consider "safe" for use. For instance would `~` be safe ? does it have have a specific usage in any part of the language (In `R` it's used for formulas). – moodymudskipper Apr 12 '18 at 13:58
  • You are right, I edited to add `macro` in tags title and description. – moodymudskipper Apr 12 '18 at 13:59
  • Actually you can include `=` in the value of a parameter, with some care. `%fun(param1=a=b)` will work and the value of `param1` will be `a=b`. – Tom Apr 12 '18 at 14:00
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/168847/discussion-between-moody-mudskipper-and-tom). – moodymudskipper Apr 12 '18 at 14:00
  • I think your question is "what characters are not used in any SAS languages?" I think there are too many SAS languages (and the list is growing), for this to be answerable. Of the symbols you have written, I do not know a SAS language where `§` or `°` are used, but there are many SAS languages I don't know. Of course your macro will probably only generate code in one language (DATA step, SQL, IML, DS2, CASL, ...). If you show your macro, and the context in which it will be used, it will be easier to identify "safe" characters. – Quentin Apr 12 '18 at 15:23
  • Unfortunately, list questions like this are not on-topic for stackoverflow (particularly because of the fact that they end up like the below). They're a better fit for communities.sas.com – Joe Apr 12 '18 at 15:40
  • For now I mainly do data wrangling, so indeed `DATA` steps, `PROC SQL` `PROC DATASETS` `PROC SUMMARY` etc... 2 instances of what I want to achieve are: (1) circumvent quoting issues of passing `a=b` by position by making my macro collection accept `a~b` instead. (imagine just for the example a macro `rename` with a `data` and `rename` parameter for instance). (2) being able to pass lists of lists as parameters by using a secondary separator, such as `§` in `a b c § d e h` (two lists of three elements). – moodymudskipper Apr 12 '18 at 15:40
  • 1
    @Moody_Mudskipper You should open questions for the specific things you're doing, then, with specific code and examples. Much of what you're talking about is well solved problems in the SAS community. – Joe Apr 12 '18 at 16:05
  • The questions I'll have for my specific examples will be "how can I make it with good practice" and "what kind of bad surprise awaits me". They'd be closed as too broad as they should be. As I see it, this is an objective question and @user667489 gave me a great objective answer. – moodymudskipper Apr 12 '18 at 16:24
  • The specific questions would be "I am doing this XYZ thing, and here is the example code, how can I avoid ". Those are good stack overflow questions. This is a list question - and if not, it's asking for offsite documentation - both of which are off topic explicitly, not because they're bad questions but because they're bad fits for how the stack overflow model works. – Joe Apr 12 '18 at 16:28
  • I understand your point and it's true that the answer I accepted contains mainly links. As for list questions I don't think guidelines are clear cut as far as I see (see https://meta.stackexchange.com/questions/11760/whats-the-opinion-on-list-questions and linked question), the issues are more about objectivity and answers staying valid with time. Anyway I appreciate your insights I'll remember them next time I have a similar issue. – moodymudskipper Apr 12 '18 at 16:41

4 Answers4

3

A bit of general reference:

I think the vast majority of the characters on a standard English keyboard are used somewhere or other in the SAS language.

To address your examples:

  • $ Used in format names, put/input statements, regular expression definitions...
  • ! 'or' operator in some environments
  • ~ 'not' operator
  • § Not used as far as I know
  • {} Can be used for data step array references & definitions
  • ° Not used as far as I know

None of the above do anything special in a macro context, as Tom has already made clear in his answer.

moodymudskipper
  • 46,417
  • 11
  • 121
  • 167
user667489
  • 9,501
  • 2
  • 24
  • 35
2

Maybe SAS Operators in Expressions can help you for ~, looking at the tables
Comparison Operators and
Logical Operators

hoila
  • 153
  • 1
  • 7
1

The main triggers in macro code are & and % which are used to trigger macro variable references and macro statements, functions or macro calls.

The ; (semi-colon) is used in macro code (as in SAS code) to indicate the end of a statement.

For passing parameters into macro parameters you mainly need to worry about , (comma). But you will also want to avoid unbalanced (). You should avoid use = when passing parameter values by position.

You can protect them by adding quotes or extra () around the values. But those characters become part of the value passed. You can use macro quoting to protect them.

 %mymac(parm1='1,200',parm2=(1,200),parm3=%str(1,200),parm4="a(b")

Equal signs can be included without quoting as long as your call is using named parameters.

 %mymac(parm1=a=b)
moodymudskipper
  • 46,417
  • 11
  • 121
  • 167
Tom
  • 47,574
  • 2
  • 16
  • 29
0

In addition to the previous answers;

  • % is also used to include files in your program. %include.

  • Using special characters may cause your code to get stuck in a loop due to unbalanced quotes. SAS Note.

If you run into this just submit the magic string below:

*';*";*/;run;
momo1644
  • 1,769
  • 9
  • 25
  • I was looking for such magic string, I'm stuck in these loops several times a day and it's bad for my nerves :). I will test it whenever possible (and upvote if it works well). I was also throwing some `)` `%mend` and `end` in there but maybe it's not necessary. – moodymudskipper Apr 12 '18 at 15:01
  • SAS Enterprise Guide used to add this string to the beginning of its auto generated code for the loop scenarios. – momo1644 Apr 12 '18 at 15:03
  • Actually I just saw in the log that my EG uses it `;*';*";*/;quit;run;` , but it's not enough and I still get stucked in situations that force me to restart the software and rerun all my unsaved work, very frustrating. – moodymudskipper Apr 13 '18 at 07:58