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

Hi, I want to create variable flag which will give Y if number from variable A will be in variable B

ABFlag
21;2;3;4;5Y

 

I've tried index, find but nothing works...

 

thank you in advance

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Example data

Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the <> icon or attached as text to show exactly what you have and that we can test code against.

 

And the actual code you are attempting.

 

Note: if you have LEADING spaces in a value like "     2", then you won't find the value in "1;2;3" because the string you search does not have the leading space. If you have trailing non-printable characters other than spaces you will have the same problem.

data example;
   a='2';
   a2 = ' 2';
   a3=cats('2',"09"x);
   b='1;2;3;4;5';
   first= index(b,a);
   second= index(b,a2);
   third= index(b,a3);
run;

The A3 value has a TAB character after the 2. When you print or examine the data in a SAS table viewer you won't see the tab and might think that is all there is to the value. But it does not match the '2' in the searched string.

 

So specific data is needed to diagnose what is going on. Since you say it finds "5" I suspect something along the lines of intermittent values with leading spaces.

View solution in original post

8 REPLIES 8
ballardw
Super User

Is A numeric or character?

If it is numeric then how did you create a character version to use the Index function with?

If you try to use a numeric variable in the Index function then the conversion to character will use a default format and usually results in leading blanks. So the value almost never is found.

You need to control the conversion to character as in:

data junk;
   a=2;
   b="1;2;3;4;5";
   flag = index(b,a);
   flagb = index(b,strip(put(a,best.)));
run;

Flag is 0, i.e. a not found because of the conversion issue.

Flagb is 3, i.e. the 2 was found in the 3rd position.

 

Personally I would use

data junk;
   a=2;
   b="1;2;3;4;5";
   flag = ( index(b,strip(put(a,best.))) > 0);
run;

Which creates a numeric value of 1, for true, and 0 for false on the find. A custom format could be used if you really need to see "Y". The coding I propose is more flexible for a number of calculations: Sum of flag is the number of 1, mean is the percent of 1s and you can write expressions like "if flag then <do something>" as SAS will treat 1 as true.

Jedrzej
Obsidian | Level 7

A and B are char not numeric

Shmuel
Garnet | Level 18

Is variable A numeric or character ?

I suppose variable B is char type.

 

If A is numeric transform it into char by strip(put(a,best8.));

then you can use any of the next functions:

1) if index(b,a) then flag='Y';

2) if findw(b,a) then flag='Y';

 

Jedrzej
Obsidian | Level 7

i don't know why it doesn't work on my dataset, when i do some dummy dataset it's ok.

 

index finds only the last number of sequence, for example 1;2;3;4;5 it gives Y flag to 5 but when it's for example 3 it does not.

 

Maybe it's something to do with delimiters?

ballardw
Super User

Example data

Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the <> icon or attached as text to show exactly what you have and that we can test code against.

 

And the actual code you are attempting.

 

Note: if you have LEADING spaces in a value like "     2", then you won't find the value in "1;2;3" because the string you search does not have the leading space. If you have trailing non-printable characters other than spaces you will have the same problem.

data example;
   a='2';
   a2 = ' 2';
   a3=cats('2',"09"x);
   b='1;2;3;4;5';
   first= index(b,a);
   second= index(b,a2);
   third= index(b,a3);
run;

The A3 value has a TAB character after the 2. When you print or examine the data in a SAS table viewer you won't see the tab and might think that is all there is to the value. But it does not match the '2' in the searched string.

 

So specific data is needed to diagnose what is going on. Since you say it finds "5" I suspect something along the lines of intermittent values with leading spaces.

Jedrzej
Obsidian | Level 7

i had trailing blanks in var A, thank you

Shmuel
Garnet | Level 18

what is the length of variable A ?

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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
  • 8 replies
  • 1958 views
  • 0 likes
  • 3 in conversation