DATA Step, Macro, Functions and more

Macro variable with hyphen used in scan function

Accepted Solution Solved
Reply
Occasional Contributor akj
Occasional Contributor
Posts: 10
Accepted Solution

Macro variable with hyphen used in scan function

I have a macro variable that contains hyphens and which are not correctly read by the scan function.  The scan function treats the hyphens as delimiters.  How do I stop this?  My guess is that it is a modifier in the scan function, but which one?

 

rx_list looks like this: ACE ACE-ARB ACE-DIU ARB ARB-ACE-DIU ARB-DIU

instead of reading it in as 6 different values, it creates 11 in the following macro.

 

 

proc sql noprint;


  select distinct name into : rx_list by ' ' from orig_data;


quit;





%macro rx_pre;


  %local I next_rx;


  %let I=1;


  %do %while (%scan(rx_list, &i) ne);


    %let next_rx=%scan(&rx_list, &i);


       ... data step ...


    %let i=&eval(&i+1);


  %end;


%mend;





%rx_pre;

Accepted Solutions
Solution
‎11-22-2017 06:18 PM
Super User
Super User
Posts: 7,860

Re: Macro variable with hyphen used in scan function

[ Edited ]

Hyphen is one of the many default delimiters that %SCAN() will use if you do not give it the delimiter you want to use.

Tell %SCAN() that you only want to use space as the delimiter.

%do %while (%scan(rx_list, &i,%str( )) ne);

Or use some other delimiter.

...
into :rx_list separated by '|' 
...
%scan(&rx_list,&i,|)

 

View solution in original post


All Replies
Super User
Posts: 22,874

Re: Macro variable with hyphen used in scan function

The default delimiter in SCAN is spaces. You can specify custom delimiters using the third parameter option.

Super Contributor
Posts: 478

Re: Macro variable with hyphen used in scan function

Hi,

 

In your proc step SEPARATED is missing

Thanks,
Suryakiran
Occasional Contributor akj
Occasional Contributor
Posts: 10

Re: Macro variable with hyphen used in scan function

Posted in reply to SuryaKiran
Thanks. It got lost in the transcription.
Solution
‎11-22-2017 06:18 PM
Super User
Super User
Posts: 7,860

Re: Macro variable with hyphen used in scan function

[ Edited ]

Hyphen is one of the many default delimiters that %SCAN() will use if you do not give it the delimiter you want to use.

Tell %SCAN() that you only want to use space as the delimiter.

%do %while (%scan(rx_list, &i,%str( )) ne);

Or use some other delimiter.

...
into :rx_list separated by '|' 
...
%scan(&rx_list,&i,|)

 

Occasional Contributor akj
Occasional Contributor
Posts: 10

Re: Macro variable with hyphen used in scan function

I tried the '|' and the first iteration of the macro works fine, but it gets hung up on the second with an error message: "A character operand was found in the %eval function or %IF condition where a numeric operand is required. The condition was %scan(&rx_list, &i, '|') ne"

 

 

Super User
Super User
Posts: 7,860

Re: Macro variable with hyphen used in scan function

Why did you include quotes in the list of delimiters you passed to the %SCAN() function?  Do you actually have quote characters in your data?

Occasional Contributor akj
Occasional Contributor
Posts: 10

Re: Macro variable with hyphen used in scan function

No, I don't and removing them seems to have solved the issue.  Thank you.

Super Contributor
Posts: 478

Re: Macro variable with hyphen used in scan function

Hi,

 

Try something like this:

 

data have;
infile datalines;
input name $20.;
datalines;
ACE
ACE-ARB
ACE-DIU
ARB
ARB-ACE-DIU
ARB-DIU
;
run;

proc sql ;
select name,count(*) into : rx_list separated by "|" ,:count
from have;
quit;

%macro rx_pre;
DATA test;
%do i=1 %to &count;
%let next_rx=%scan(&rx_list,&i,"|");
Var_&i="&next_rx.";
%end;
RUN;
%mend;
%rx_pre;

Thanks,
Suryakiran
Occasional Contributor akj
Occasional Contributor
Posts: 10

Re: Macro variable with hyphen used in scan function

Posted in reply to SuryaKiran
Thank you for the suggestion, but I am getting the values from a dataset.
Super User
Posts: 22,874

Re: Macro variable with hyphen used in scan function

@akj The first step is in the solution to create fake data to demonstrate how the code works. It’s known as a fully worked example/reproducible example. 

 

We don’t have your data or code to work with, so we can either make suggestions or make demo data to work with. Personally, I won’t take the time to make demo data anymore, but @SuryaKiran has take the time to make demo data to demonstrate a solution to your problem. 

 

 

Super User
Super User
Posts: 7,860

Re: Macro variable with hyphen used in scan function

Posted in reply to SuryaKiran

Why did you include quotes in the list of delimiters you passed to the %SCAN() function?

%macro rx_pre;
DATA test;
%do i=1 %to &count;
%let next_rx=%scan(&rx_list,&i,|);
 Var_&i="&next_rx.";
%end;
RUN;
%mend;

 

 

 

Super User
Super User
Posts: 7,860

Re: Macro variable with hyphen used in scan function

Make sure to use the right delimiter list. It is easier to use an iterative do loop.

%let rx_list=ACE ACE-ARB ACE-DIU ARB ARB-ACE-DIU ARB-DIU;
%do i=1 %to %sysfunc(countw(&rx_list,%str( ));
   %let next=%scan(&rx_list,&i,%str( ));
%end;
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 12 replies
  • 186 views
  • 0 likes
  • 4 in conversation