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

good day,

 

here is my program.

 

just wondering why if the up and down row are exact the same. the result always = 40?

 

is it something wrong with my program? word "checking" only 8 character.

 

data testing2;
input name $40.;
infile datalines dlm=',';
datalines;
checking
checking
checking
checking
checking1
checking2
;
run;

 

data testing3;
set testing2;
name2=compress(name);
lag_name=compress(lag(name));
run;

 

data testing4;
set testing3;

if substr(compress(name2),1,40) = substr(compress(lag_name),1,40) then Length_match_Lag ="40";
else if substr(compress(name2),1,9) = substr(compress(lag_name),1,9) then Length_match_Lag ="9";
else if substr(compress(name2),1,8) = substr(compress(lag_name),1,8) then Length_match_Lag ="8";

run;

 

thanks in advance

harry

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

compress() strips all leading and trailing blanks, so the resulting string is only 8 or 9 bytes long. This lets the checks for 40 (in all cases) and 9 (in most other cases) fail. The substr() function issues a NOTE (Maxim 2: Read the Log) and returns blank values, which match in the check for 40.

 

You are better off using a do loop to determine the match length:

data testing2;
input name $40.;
infile datalines dlm=',';
datalines;
checking
checking
checking
checking
checking1
checking2
checking2
;
run;
 
data testing4;
set testing2;
name2=compress(name);
lag_name=compress(lag(name));
do i = 1 to length(name2);
  if substr(name2,1,i) = substr(lag_name,1,i) then Length_match_Lag = i;
end;
drop i;
run;

I added a second "checking2" value to get one result of 9.

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User

compress() strips all leading and trailing blanks, so the resulting string is only 8 or 9 bytes long. This lets the checks for 40 (in all cases) and 9 (in most other cases) fail. The substr() function issues a NOTE (Maxim 2: Read the Log) and returns blank values, which match in the check for 40.

 

You are better off using a do loop to determine the match length:

data testing2;
input name $40.;
infile datalines dlm=',';
datalines;
checking
checking
checking
checking
checking1
checking2
checking2
;
run;
 
data testing4;
set testing2;
name2=compress(name);
lag_name=compress(lag(name));
do i = 1 to length(name2);
  if substr(name2,1,i) = substr(lag_name,1,i) then Length_match_Lag = i;
end;
drop i;
run;

I added a second "checking2" value to get one result of 9.

Tom
Super User Tom
Super User

What is the test you are tying to do?  Why are you using COMPRESS()?  Are you trying to remove the embedded spaces from the values?  Why do you keep calling it after you already removed them?  Did you expect them re-appear somehow?

data test;
  input name $40.;
  lag_name = lag(name);
  match40 = name = lag_name;
  match9 = substr(name,1,9) = substr(lag_name,1,9);
  match8 = substr(name,1,8) = substr(lag_name,1,8);
datalines;
checking
checking
checking
checking
checking1
checking2
checking2
;
proc print;
run;
Obs      name       lag_name     match40    match9    match8

 1     checking                     0          0         0
 2     checking     checking        1          1         1
 3     checking     checking        1          1         1
 4     checking     checking        1          1         1
 5     checking1    checking        0          0         1
 6     checking2    checking1       0          0         1
 7     checking2    checking2       1          1         1

 

harrylui
Obsidian | Level 7

thanks you all

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 678 views
  • 2 likes
  • 3 in conversation