Amit Kumar
Amit Kumar

Reputation: 3

Is there any function in SAS where we can read the exact value from the variable

Suppose i have a column called ABC and that variable has the data like :

  123_112233_66778_1122 or
  123_112233_1122_11232 or 
  1122_112233_66778_123

so i want to generate the desire variable in the next column as 1122. like this "1122" i have a long list where i need to cross the value from the column called ABC, if found the exact match then need to generate. However, i don't want to generate the match like 112233 because it does not match the value what i am looking for.

For an example you can see all three line what i have given for reference. I am taking only the match records which is "1122" from all the above 3 lines.

I really have no clue to overcome on the problem. I have tried my hands with wildcards but did not get much success. Any help would be much apricated

Upvotes: 0

Views: 593

Answers (1)

Tom
Tom

Reputation: 51611

It is hard to tell from your description, but from the values you show it looks like you want the INDEXW() function. That will let you search a string for matching words with a option to specify which characters are to be considered as the separators between the words. The result is the location of where the word starts within longer string. When the word is not found the result is a zero.

Let's create a simple example to demonstrate.

data have;
  input abc $30. ;
cards;
123_112233_66778_1122
123_112233_1122_11232
1122_112233_66778_123
;

data want;
  set have ;
  location = indexw(trim(abc),'1122','_');
run;

Note that SAS will consider any value other than zero (or missing) as TRUE so you can just use the INDEXW() function call in a WHERE statement.

data want;
  set have;
  where indexw(trim(abc),'1122','_');
run;

Upvotes: 1

Related Questions