ponyhd
ponyhd

Reputation: 521

SAS split macro string with delimiter

I would like to give variables proper names in SAS macro, however, I either got error message or got first letter of the string. Here is an example using following SAS codes:

data trial; 
  %let cate=gender age;
  %let label="Gender*Age at dx";
    do i=1 to countw("&cate");
      item=scan("&cate",i) ;  
      print=scan(%str(&label),i,*);
      output;
   end;
run;

I got error like this:

ERROR 386-185: Expecting an arithmetic expression. 25385 print=scan(%str(&label),i,*);

ERROR 200-322: The symbol is not recognized and will be ignored.

ERROR 76-322: Syntax error, statement will be ignored.

If I change the codes like this:

data trial;
  %let cate=gender age;
  %let label="Gender*Age at dx";
    do i=1 to countw("&cate");
      item=scan("&cate",i) ;  
      print=scan(%str(&label),i);
      output;
   end;
run;

I get print=Age instead of print='Age at dx'. Any hints on this? Thanks!

Upvotes: 0

Views: 1047

Answers (1)

Stu Sztukowski
Stu Sztukowski

Reputation: 12849

You are missing quotation marks around your delimeter, *, in the scan() function. You also do not need to use %str().

print=scan(&label, i, '*');

Output:

i   item    print
1   gender  Gender
2   age     Age at dx

I would also recommend placing your %let statements before your data step to make it a bit easier to read.

%let cate=gender age;
%let label="Gender*Age at dx";

data trial; 
    ...;
run;

Upvotes: 1

Related Questions