Help using Base SAS procedures

array / string in macro variable

Reply
Occasional Contributor
Posts: 15

array / string in macro variable

Hello,

this assingment works in sas.
%let arr = 'aaa', 'bbb', 'ccc';

The "arr" variable contains the array of 3 strings? Or it contains only one long string?

Because when i pass the "arr" variable to %substr function, it fail because the variable contains comma, which is also separator in function arguments.

My question is
- in case if arr variable is array of strings, how can I reference particular items?
- in case it is only one string, how can i handle special character (comma) in the string?

Thanks for an answer
Super Contributor
Super Contributor
Posts: 3,174

Re: array / string in macro variable

Explain exactly how you expect to use the data - what type of SAS code, DATA step, PROC SQL, MACRO usage? Also, provide a SAS-generated log where you are using the string with your %LET revealed and all other code, embedded in a SAS program - use COPY / PASTE for your log to add to a forum post-reply.

Scott Barry
SBBWorks, Inc.
Super User
Posts: 11,343

Re: array / string in macro variable

Your ARR contains a single string.

Before dealing with options on handling, it does help to how the how thing is used. For most purposes in a macro both the quotes around your values and the commas are likely to be unneeded.
Super Contributor
Super Contributor
Posts: 3,174

Re: array / string in macro variable

Though data-strings can be addressed in various methods using SAS, I have to say that sometimes the specific intent is to provide the quoted-string and use it somehow where such a data-value is supplied, such as using a WHERE statement with an "IN" operator.

On occasion, a "flower boxed" %LET statement is used to reveal a "prompt like" interaction from an end-user who has been given a self-contained SAS program, where the only interaction is to "fill in the desired filter values, in quote-marks".

Scott Barry
SBBWorks, Inc.
Regular Contributor
Posts: 241

Re: array / string in macro variable

...

> this assingment works in sas.

> %let arr = 'aaa', 'bbb', 'ccc';

>

> The "arr" variable contains the array of 3 strings?

> Or it contains only one long string?



It is a value that is assigned to a macro variable. Macro variables store text, or a string of characters.



> Because when i pass the "arr" variable to %substr

> function, it fail because the variable contains

> comma, which is also separator in function

> arguments.



Yes, this is expected.



> My question is

...

> - in case it is only one string, how can i handle

> special character (comma) in the string?



By quoting, that is, hiding the comma's from macro processor.



   %let arr = %str('aaa', 'bbb', 'ccc');


   %put first word = %scan(&arr, 1, %str(,));


   %put second word = %scan(&arr, 2, %str(,));


   %*-- on log


   first word = 'aaa'


   second word =  'bbb'


   --*;

Ask a Question
Discussion stats
  • 4 replies
  • 242 views
  • 0 likes
  • 4 in conversation