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;
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;
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;
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;
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.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.