DATA Step, Macro, Functions and more

scan and findw function

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

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 ]
Posted in reply to himanshu1

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));

View solution in original post


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

Re: scan and findw function

[ Edited ]
Posted in reply to himanshu1

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

Re: scan and findw function

Posted in reply to Astounding

thanks alot for explaining the logic. now im able to uderstand the flow how its going. once again thank you.

☑ This topic is solved.

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

Discussion stats
  • 2 replies
  • 477 views
  • 0 likes
  • 2 in conversation