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

I want to create a dataset from a list because merging to a proc sql or sql pass through might be easier to manage. I know this must be simple but it doesn't like the scan.  The list is separated by commas. 

 


data druglist ;
format drug $200. ;
%let ct=%sysfunc(countw(%quote(&druglist.), %str(,) )) ;
%put &ct. ;
do i = 1 to &ct ;
drug = scan(&druglist, i) ;
output ;
end ;
run;
proc print ; run;

 

 

log:

 

24 data druglist ;
25 format drug $200. ;
26 %let ct=%sysfunc(countw(%quote(&druglist.), %str(,) )) ;
27 %put &ct. ;
6
28 do i = 1 to &ct ;
29 drug = scan(&druglist, i) ;
____
72
ERROR 72-185: The SCAN function call has too many arguments.

30 output ;
31 end ;
32 run;

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

You're mixing macro and data step code:

 

data druglist ;

format drug $200. ;

 ct=countw("&druglist") ;

do i = 1 to ct ;
    drug = scan("&druglist", i) ;
    output ;
end ;

run;

proc print data=druglist;
run;

View solution in original post

4 REPLIES 4
SuryaKiran
Meteorite | Level 14

Double quote the list for macro variable.

 

data druglist ;
format drug $200. ;
%let ct=%sysfunc(countw(%quote(&druglist.), %str(,) )) ;
%put &ct. ;
do i = 1 to &ct ;
drug = scan("&druglist", i) ;
output ;
end ;
run;
proc print ; run;

Thanks,
Suryakiran
Reeza
Super User

You're mixing macro and data step code:

 

data druglist ;

format drug $200. ;

 ct=countw("&druglist") ;

do i = 1 to ct ;
    drug = scan("&druglist", i) ;
    output ;
end ;

run;

proc print data=druglist;
run;
CP2
Pyrite | Level 9 CP2
Pyrite | Level 9
omg - working with too many macros and proc sqls lately I guess. Thank you!
ballardw
Super User

If your "druglist" macro variable has commas then that will cause the error. If your variable has individually quoted values then you'll get a different error.

 

it would help to show what your "druglist" looks like.

And you are way overusing macro functions where not needed and incorrect.

 

%let druglist = a bdq cccccc dddddddddd;
data example ;
do i = 1 to (countw("&druglist"));
   drug = scan("&druglist.", i) ;
   output;
end; ;
run;

You should specify a length for DRUG as otherwise the length is going to be the length of the macro variable.

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 4 replies
  • 691 views
  • 0 likes
  • 4 in conversation