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

I am new in sas area, I run below code got error message as below, please help me figure out that, thanks,

NOTE: Line generated by the invoked macro "LOOP".

7 select count (distinct presc_ord_npi) as &value1 from bhrx_presc as a where

----------

22

7 ! gnrc_name like "&value"; quit;

ERROR 22-322: Syntax error, expecting one of the following: ',', AS, FROM.

 

code:

%macro loop(values);   
       %let count=%sysfunc(countw(&values));                                                                                       
     %do i = 1 %to &count;                                                                                                             
      %let value=%qscan(&values,&i,%str(,));  

%let value=%sysfunc(dequote(&value));
  %put &value;
    %let value1= %qscan(&value,1);
   proc sql;
select count (distinct presc_ord_npi) as &value1  from bhrx_presc as a where gnrc_name like "&value";
      quit;
     %end;                                                                                                                                                                                                            %mend;                                                                                                                                        
                                                                    
%loop(%str('METHYLPHENIDATE HCL','DEXTROAMPHETAMINE/AMPHETAMINE,CLONIDINE HCL'));

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

Ok, now you really made me read your code. 

The problem is in your count, Try the below correction and let me know if that works

 

%macro loop(values);
%let count=%sysfunc(countw(&values,%str(,)));
%put c=&count;
%do i = 1 %to &count;
%let value=%qscan(&values,&i,%str(,));

%let value1= %qscan(&value,1);
%put value1=&value1;
proc sql ;
select count (distinct presc_ord_npi) as &value1 from bhrx_presc as a
where gnrc_name like "&value";
quit;
%end;
%mend;
%loop(%str(METHYLPHENIDATE HCL,DEXTROAMPHETAMINE/AMPHETAMINE,CLONIDINE HCL));

View solution in original post

8 REPLIES 8
Astounding
PROC Star

It's not clear what you would like VALUE1 to contain.  But it is clear that the problem lies  in that area.  You are getting unbalanced single quotes within the value for VALUE1, since %SCAN does not use quotes as delimiters.  What are there any quotes at all in your original list?

babykxy
Calcite | Level 5

thank you for you help!!!

VALUE1 is for result part I can get count name as VALUE1,

ex:

The SAS System

METHYLPHENIDATE
7879

 

 

there are not quotes l in my original list, but even I remove quotes from list still got same error: 

 

 

NOTE: Line generated by the invoked macro "LOOP".

11 select count (distinct presc_ord_npi) as &value1 from bhrx_presc as a

----------

22

11 ! where gnrc_name like "&value"; quit;

ERROR 22-322: Syntax error, expecting one of the following: ',', AS, FROM.

NOTE: The SAS System stopped processing this step because of errors.

NOTE: PROCEDURE SQL used (Total process time):

 

 

 

code:

%macro loop(values);                                                                                                                                      
     %let count=%sysfunc(countw(&values));                                                                                       
     %do i = 1 %to &count;                                                                                                             
      %let value=%qscan(&values,&i,%str(,)); 
  %put &value;
    %let value1= %qscan(&value,1);
   proc sql;
select count (distinct presc_ord_npi) as &value1  from bhrx_presc as a where gnrc_name like "&value";
      quit;
     %end;                                                                                                                                                                                                                                                                   
%mend;                                                                                                                                                                                                            
%loop(%str(METHYLPHENIDATE HCL,DEXTROAMPHETAMINE/AMPHETAMINE,CLONIDINE HCL));

novinosrin
Tourmaline | Level 20

your macro call has unmatched quotes in your parameters

 

%loop(%str('METHYLPHENIDATE HCL','DEXTROAMPHETAMINE/AMPHETAMINE,CLONIDINE HCL'));

 

should be:

 

%loop(%str('METHYLPHENIDATE HCL','DEXTROAMPHETAMINE/AMPHETAMINE','CLONIDINE HCL'))

 

 

Also, I don't see any reason in your code to have the quotes in your parameter in the first place, then quote the scanned result with a qscan and then dequote and then call in your proc sql. 

babykxy
Calcite | Level 5

thank you so much!

you are right! I remove quote from list, also dequote statement, but program still have same error message!

 

%macro loop(values);                                                                                                                                      
     %let count=%sysfunc(countw(&values));                                                                                       
     %do i = 1 %to &count;                                                                                                             
      %let value=%qscan(&values,&i,%str(,)); 
  %put &value;
    %let value1= %qscan(&value,1);
   proc sql;
select count (distinct presc_ord_npi) as &value1  from bhrx_presc as a where gnrc_name like "&value";
      quit;
     %end;                                                                                                                                                                                                                                                                    
%mend;                                                                                                                                        
%loop(%str(METHYLPHENIDATE HCL,DEXTROAMPHETAMINE/AMPHETAMINE,CLONIDINE HCL));

 

NOTE: Line generated by the invoked macro "LOOP".

11 select count (distinct presc_ord_npi) as &value1 from bhrx_presc as a

                                                                                             ----------

                                                                                                22

11 ! where gnrc_name like "&value"; quit;

ERROR 22-322: Syntax error, expecting one of the following: ',', AS, FROM.

NOTE: The SAS System stopped processing this step because of errors.

NOTE: PROCEDURE SQL used (Total process time):

real time 0.00 seconds

cpu time 0.00 seconds

 

 

 

 

novinosrin
Tourmaline | Level 20

Ok, now you really made me read your code. 

The problem is in your count, Try the below correction and let me know if that works

 

%macro loop(values);
%let count=%sysfunc(countw(&values,%str(,)));
%put c=&count;
%do i = 1 %to &count;
%let value=%qscan(&values,&i,%str(,));

%let value1= %qscan(&value,1);
%put value1=&value1;
proc sql ;
select count (distinct presc_ord_npi) as &value1 from bhrx_presc as a
where gnrc_name like "&value";
quit;
%end;
%mend;
%loop(%str(METHYLPHENIDATE HCL,DEXTROAMPHETAMINE/AMPHETAMINE,CLONIDINE HCL));

novinosrin
Tourmaline | Level 20

Ok good I am glad. @babykxy May i request you to mark the question as answered and close the thread if you don't mind plz

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

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

View all other training opportunities.

Discussion stats
  • 8 replies
  • 30486 views
  • 0 likes
  • 3 in conversation