Desktop productivity for business analysts and programmers

Peculiar behaviour of find()?

Accepted Solution Solved
Reply
Contributor
Posts: 70
Accepted Solution

Peculiar behaviour of find()?

[ Edited ]

Data Test3 ;
Set Test2 ;
Format o_String $2000. ;
if _N_ = 1 then do;
O_String = "" ;
end ;
O_String = catx(" , ", O_String, " ") ;
find = Find(O_String,O_Level) ;
O_String = catx(" , ", O_String, O_Level) ;
new = Find(O_String,O_Level) ;
Retain O_String ;
Run ;

 

I am confused as why the column named find is always showing zero when the column named new is showing the correct value.

As per my understanding only the first row in find should show  a blank value otherwise it should also give some value but it is always giving a straight zero.

 

Can anyone help me with this..??


Accepted Solutions
Solution
‎08-30-2017 02:06 AM
Super User
Posts: 9,611

Re: Peculiar behaviour of find()?

[ Edited ]
Posted in reply to DipeshGupta

When assigning find, you are testing for the presence of o_level before you append it to o_string, so find will be zero unless the same value for o_level was already on the dataset.

But you also made the mistake of assuming that a shorter string will consist of less characters for SAS, which it doesn't. The find() function will take the padding of o_level with blanks into account, and only find a position if the visible value of o_level can be found padded with blanks in o_string:

data test2;
input o_level $;
cards;
xxx
yyy
zzz
xxx
;
run;

data test3;
set test2;
format o_string $2000.;
retain o_string "";
o_string = catx(" , ",o_string," ");
find = find(o_string,trim(o_level));
o_string = catx(" , ",o_string,o_level);
new = find(o_string,o_level);
run;

So I used the trim() function in the first test.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

View solution in original post


All Replies
Solution
‎08-30-2017 02:06 AM
Super User
Posts: 9,611

Re: Peculiar behaviour of find()?

[ Edited ]
Posted in reply to DipeshGupta

When assigning find, you are testing for the presence of o_level before you append it to o_string, so find will be zero unless the same value for o_level was already on the dataset.

But you also made the mistake of assuming that a shorter string will consist of less characters for SAS, which it doesn't. The find() function will take the padding of o_level with blanks into account, and only find a position if the visible value of o_level can be found padded with blanks in o_string:

data test2;
input o_level $;
cards;
xxx
yyy
zzz
xxx
;
run;

data test3;
set test2;
format o_string $2000.;
retain o_string "";
o_string = catx(" , ",o_string," ");
find = find(o_string,trim(o_level));
o_string = catx(" , ",o_string,o_level);
new = find(o_string,o_level);
run;

So I used the trim() function in the first test.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 9,611

Re: Peculiar behaviour of find()?

Posted in reply to DipeshGupta

I also edited your subject line. "SAS Datastep" is very uninformative with respect to your question.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 70

Re: Peculiar behaviour of find()?

Posted in reply to KurtBremser

Thankyou So Much.

It got it and the code worked.Smiley Happy

☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 263 views
  • 1 like
  • 2 in conversation