BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
belboy
Obsidian | Level 7

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,

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

3 REPLIES 3
Reeza
Super User

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

ballardw
Super User

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;
belboy
Obsidian | Level 7

THanks!!

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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