## scan and findw function

Solved
Occasional Contributor
Posts: 6

# scan and findw function

data One;
input Date_Info \$ 20.;
cards;
24Nov is Sunday
25Nov is Monday
26Nov is Tuesday
27Nov is Wednesday
28NOV IS THURSDAY
29nov is Friday
30NOV is SATURDAY
;
run;

data two;
set one;
a=scan(date_info,-1);
put a;
d=trim(a);
c=findw(date_info,d);
put c;
run;

Iam writing this code it giving c as zero always why?

Accepted Solutions
Solution
‎08-27-2017 11:17 AM
Super User
Posts: 6,934

## Re: scan and findw function

[ Edited ]

In SAS data sets, variables have a fixed length.  That length does not change from one observation to the next.  Therefore ...

When you assign a value to A, SAS defines A as character, with a length of 20.  It is possible that 20 characters would be needed to store A, so that's the length assigned.

Similarly, when you define D, SAS defines it as character with a length of 20.  Again, it is possible that 20 characters would be needed to store its value, so that's the length assigned.

When the TRIM function executes, it does remove trailing blanks.  However, the result gets stored in a variable that is defined as 20 characters long, so any blanks that were removed by TRIM get added back on again.  As a result, FINDW cannot find that 20-character string with all those trailing blanks.

To remedy the situation, try:

c = findw(date_info, trim(a));

All Replies
Solution
‎08-27-2017 11:17 AM
Super User
Posts: 6,934

## Re: scan and findw function

[ Edited ]

In SAS data sets, variables have a fixed length.  That length does not change from one observation to the next.  Therefore ...

When you assign a value to A, SAS defines A as character, with a length of 20.  It is possible that 20 characters would be needed to store A, so that's the length assigned.

Similarly, when you define D, SAS defines it as character with a length of 20.  Again, it is possible that 20 characters would be needed to store its value, so that's the length assigned.

When the TRIM function executes, it does remove trailing blanks.  However, the result gets stored in a variable that is defined as 20 characters long, so any blanks that were removed by TRIM get added back on again.  As a result, FINDW cannot find that 20-character string with all those trailing blanks.

To remedy the situation, try:

c = findw(date_info, trim(a));

Occasional Contributor
Posts: 6