DATA Step, Macro, Functions and more

looping through a list to create small dataset

Accepted Solution Solved
Reply
Frequent Contributor
Frequent Contributor
Posts: 139
Accepted Solution

looping through a list to create small dataset

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;


Accepted Solutions
Solution
‎03-08-2018 12:05 PM
Super User
Posts: 23,998

Re: looping through a list to create small dataset

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


All Replies
PROC Star
Posts: 629

Re: looping through a list to create small dataset

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
Solution
‎03-08-2018 12:05 PM
Super User
Posts: 23,998

Re: looping through a list to create small dataset

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;
Frequent Contributor
Frequent Contributor
Posts: 139

Re: looping through a list to create small dataset

omg - working with too many macros and proc sqls lately I guess. Thank you!
Super User
Posts: 13,939

Re: looping through a list to create small dataset

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.

 

☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 113 views
  • 0 likes
  • 4 in conversation