BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Julia22
Fluorite | Level 6

 

I have

 

Note : XOP PM is one word and XOP DD is one word  and X/Y/N is one word

 

%macro test;

%let xy= XOP  PM , XOP DD, X/Y/N, EPP,ACC,ENN;

want to scan how many word I have

answer  should be 6

%let n=%sysfunc(countw(%bquote(&xy)));

 

%do h=1 %to &n;

%let cname = %upcase(%scan(&xy,&h.,%str(',')));

%END;

%PUT &cname;

 

%mend;

%test;

 

So first time cname should be XOP PM and second time cname should be XOP DD and third time cname should be X/Y/N

 

Can u please help

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Why are you specifying the delimiter for %SCAN() but not for COUNTW()?

Why are you quoting the commas in &XY for the COUNTW() function, but not for the %SCAN() function?

Why did you include single quote character in the list of delimiters?  If they are valid delimiters then you can use ',' as the delimiter specification and you will not need to use macro quoting since the actual quotes will prevent SAS from thinking the comma is part of the function call.

 

%macro test;
%local xy n h cname ;
%let xy= XOP  PM , XOP DD, X/Y/N, EPP,ACC,ENN;
%let n=%sysfunc(countw(%bquote(&xy),%str(,)));

%do h=1 %to &n;
  %let cname = %scan(%qupcase(&xy),&h,%str(,));
  %put H=&h CNAME="&cname";
%end;

%mend test;
6881   %test;
H=1 CNAME="XOP  PM"
H=2 CNAME="XOP DD"
H=3 CNAME="X/Y/N"
H=4 CNAME="EPP"
H=5 CNAME="ACC"
H=6 CNAME="ENN"

 

View solution in original post

3 REPLIES 3
Tom
Super User Tom
Super User

Why are you specifying the delimiter for %SCAN() but not for COUNTW()?

Why are you quoting the commas in &XY for the COUNTW() function, but not for the %SCAN() function?

Why did you include single quote character in the list of delimiters?  If they are valid delimiters then you can use ',' as the delimiter specification and you will not need to use macro quoting since the actual quotes will prevent SAS from thinking the comma is part of the function call.

 

%macro test;
%local xy n h cname ;
%let xy= XOP  PM , XOP DD, X/Y/N, EPP,ACC,ENN;
%let n=%sysfunc(countw(%bquote(&xy),%str(,)));

%do h=1 %to &n;
  %let cname = %scan(%qupcase(&xy),&h,%str(,));
  %put H=&h CNAME="&cname";
%end;

%mend test;
6881   %test;
H=1 CNAME="XOP  PM"
H=2 CNAME="XOP DD"
H=3 CNAME="X/Y/N"
H=4 CNAME="EPP"
H=5 CNAME="ACC"
H=6 CNAME="ENN"

 

Julia22
Fluorite | Level 6

Hi,

 

It worked  🙂

 

 

Thank you So much

ShiroAmada
Lapis Lazuli | Level 10

Use COUNTC function 

wordsn=sum(countc(sourcevar,","),1);

 

Then you can proceed with your code.

 

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 1404 views
  • 0 likes
  • 3 in conversation