name
name

Reputation: 41

SAS how to slice and loop through a VARCHAR

I would like to slice and loop through a VARCHAR this is what I try :

%MACRO test;

data
     %DO i_=1 %TO 4;

          %LET extract&i_.=%SCAN(&string.,&i_.," ");
          %PUT extract&i_.=&&extract&i_.._hash;

     %END;
%MEND test;

this work but the problem is that I have to put the number of words in my code for exemple here it's 4 but if I have a new string with 5 or 6 words this doesn't work.

Upvotes: 0

Views: 280

Answers (1)

Tom
Tom

Reputation: 51611

Use the COUNTW() function.

%DO i_=1 %TO %sysfunc(countw(&string.," "));

PS Are you really using quote and space as delimiter characters in the value of &STRING? That is what you are telling %SCAN() to use.

Upvotes: 2

Related Questions