BookmarkSubscribeRSS Feed
stataq
Quartz | Level 8

Hello,

 

I have silly question: what does following code try to get?

 

%scan (&_indata,1,%str( %())

it looks like it try to get the 1st segment  of &_indata.. I feel confused on the part of "%str( %()". Could anyone interpretate that for me, especially the part with % and (.

 

Thanks.

 

 

4 REPLIES 4
yabwon
Amethyst | Level 16

Maxim#1: Read the documentation

 

https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/mcrolref/p0pnc7p9n4h6g5n16g6js048nhfl.htm

 

yabwon_0-1730215911742.png

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



ballardw
Super User

The third position of the scan and %scan is the list of characters used as delimiters. So someone wanted to set the list characters to include the % sign as well as space and the parentheses as delimiters. Because the macro language uses % as a special character they needed to use the %str() to prevent using that % in the list as a macro trigger.

 

By default %scan would use any of the following as delimiters:blank ! $ % & ( ) * + , - . / ; < ^¦

So one suspects that one or more of those characters were expected to appear in the values and they did not want those others to be treated as delimiters.

Kurt_Bremser
Super User
#STR is used for masking characters which would otherwise be discarded (the blank) or have special meaning in macro language.
Since the opening parenthesis is also needed as a delimiter for %SCAN, it is also put in as an argument for %STR. But the function would treat the two consecutive opening/closing brackets as a pair, which is not wanted; therefore the bracket is masked with % to prevent this. You can find this in https://documentation.sas.com/doc/de/pgmsascdc/9.4_3.5/mcrolref/n09tblrxldh8k0n1kt6dkj3xlxug.htm (the documentation of %STR).
Tom
Super User Tom
Super User

It extracts the dataset name from a macro variable that might hold a dataset name with dataset options.

 

Example:

%let inds=sashelp.class(drop=age);
%let ds_only = %scan(&inds,1,%str( %());

Results

1    %let inds=sashelp.class(drop=age);
2    %let ds_only = %scan(&inds,1,%str( %());
3    %let libname = %upcase(%scan(work.&ds_only,-2,.));
4    %let memname = %upcase(%scan(&ds_only,-1,.));
5    %put &=inds &=ds_only &=libname &=memname;
INDS=sashelp.class(drop=age) DS_ONLY=sashelp.class LIBNAME=SASHELP MEMNAME=CLASS
6
7    %let inds=class;
8    %let ds_only = %scan(&inds,1,%str( %());
9    %let libname = %upcase(%scan(work.&ds_only,-2,.));
10   %let memname = %upcase(%scan(&ds_only,-1,.));
11   %put &=inds &=ds_only &=libname &=memname;
INDS=class DS_ONLY=class LIBNAME=WORK MEMNAME=CLASS

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 4 replies
  • 1046 views
  • 4 likes
  • 5 in conversation