DATA Step, Macro, Functions and more

SAS DO Loop with values and symbols

Accepted Solution Solved
Reply
Contributor
Posts: 23
Accepted Solution

SAS DO Loop with values and symbols

Hi,

I'm running a DO LOOP but SAS keeps deleting two observations that I need to keep in my results.  For example, this is my dataset:

 

RACF                           GROUPS

USDISEF                      MKZ;admin;FF

EOR_E47 ACCT           %

USI43_SK                     RETSIPX;dba

UT_ACCT C47              %

 

%let folder = work.IMG;
%let loop = GROUPS;


/*STEP 1 - Creates DO Loop necessary for possible combined columns in complex files.*/


data Do_&appname;
set &folder;

x = countw(&loop);
do i = 1 to x;
&loop.1 = scan(&loop, i, ";");
output;
END;
run;

 

I get the results as needed with the exception of  EOR_E47 ACCT  and   UT_ACCT C47  .  SAS excludes these from the results.  How can I get them included with a value of "%"?

 

Any help is much appreciated.

 

Thanks,


Accepted Solutions
Solution
‎04-04-2018 02:27 PM
Super User
Posts: 13,023

Re: SAS DO Loop with values and symbols

First I think you need to describe a bit more of what this is supposed to do.

 

The issue is documented in the documentation for the Countw function of "what is a word":

"word" refers to a substring that has one of the following characteristics: 
• is bounded on the left by a delimiter or the beginning of the string 
 
•is bounded on the right by a delimiter or the end of the string 
 
•contains no delimiters, except if you use the Q modifier and the delimiters are within substrings that have quotation marks 

The last one is the cause of your problem as delimiters for countw are:

 

If your computer uses ASCII characters, then the default delimiters are as follows: 
 
blank ! $ % & ( ) * + , - . / ; < ^ | 

So since your value is % that is a delimiter and countw("%",1) returns 0. And the do loop becomes:

 

do I = 1 to 0; so it never actually loops.

Since that, hopefully, would be a special case you could use something like

data Do_&appname;
   set &folder;
   x = countw(&loop);
   if x>0 then    do i = 1 to x;
   &loop.1 = scan(&loop, i, ";");
      output;
   END;
   else do;
      &loop.1=&loop;
      output;
   end;
run;

View solution in original post


All Replies
Super User
Posts: 22,844

Re: SAS DO Loop with values and symbols

What do you mean excludes them from the results? What are you getting, versus, what do you expect?

Solution
‎04-04-2018 02:27 PM
Super User
Posts: 13,023

Re: SAS DO Loop with values and symbols

First I think you need to describe a bit more of what this is supposed to do.

 

The issue is documented in the documentation for the Countw function of "what is a word":

"word" refers to a substring that has one of the following characteristics: 
• is bounded on the left by a delimiter or the beginning of the string 
 
•is bounded on the right by a delimiter or the end of the string 
 
•contains no delimiters, except if you use the Q modifier and the delimiters are within substrings that have quotation marks 

The last one is the cause of your problem as delimiters for countw are:

 

If your computer uses ASCII characters, then the default delimiters are as follows: 
 
blank ! $ % & ( ) * + , - . / ; < ^ | 

So since your value is % that is a delimiter and countw("%",1) returns 0. And the do loop becomes:

 

do I = 1 to 0; so it never actually loops.

Since that, hopefully, would be a special case you could use something like

data Do_&appname;
   set &folder;
   x = countw(&loop);
   if x>0 then    do i = 1 to x;
   &loop.1 = scan(&loop, i, ";");
      output;
   END;
   else do;
      &loop.1=&loop;
      output;
   end;
run;
Contributor
Posts: 23

Re: SAS DO Loop with values and symbols

THanks!!

☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 104 views
  • 0 likes
  • 3 in conversation