Hello experts:
The original data records the different problems that occurred in the company (recorded in numbers) and are separated by '||', as shown below.
data data;
input string$40.;
datalines;
|17|42|44|
|17|33|55|2|
|5|12|17|42|44|77|
;
run;
I want to mark the company that has 42 or 12 problems as having a big problem (big_problem) as 1, so the first and third records will be 1, and the second record will be 0.
Thanks to the experts.
You have a character variable with a list of problem identifiers, separated by pipe symbols. Treat those pipes as word separators, in the SCAN function. Use the SCAN function to examine each "word" in the string, to see if any has a value of "42" or "12":
data data;
input string$40.;
datalines;
|17|42|44|
|17|33|55|2|
|5|12|17|42|44|77|
run;
data want;
set data;
big_problem=0;
do w=1 to countw(string,'|') while (big_problem=0);
if strip(scan(string,w,'|')) in ('12','42') then big_problem=1;
end;
drop w;
run;
The STRIP function removes leading and trailing blanks, if there are any, in each constituent of the string variable.
You have a character variable with a list of problem identifiers, separated by pipe symbols. Treat those pipes as word separators, in the SCAN function. Use the SCAN function to examine each "word" in the string, to see if any has a value of "42" or "12":
data data;
input string$40.;
datalines;
|17|42|44|
|17|33|55|2|
|5|12|17|42|44|77|
run;
data want;
set data;
big_problem=0;
do w=1 to countw(string,'|') while (big_problem=0);
if strip(scan(string,w,'|')) in ('12','42') then big_problem=1;
end;
drop w;
run;
The STRIP function removes leading and trailing blanks, if there are any, in each constituent of the string variable.
@mkeintz Thank you for your assistance.
I have a question. If the company with a big problem is 2 or 42, how can I avoid 2 by catching 2 instead of catching 12 and 42?
@shawnchen0321 wrote:
@mkeintz @Thank you for your assistance.
I have a question. If the company with a big problem is 2 or 42, how can I avoid 2 by catching 2 instead of catching 12 and 42?
This is a perfect opportunity to do a test. Create a string with 12 (and no 2) and a string with 2 (and no 12). Then see whether the code generates a false positive from the string with 2.
After testing, I found that SAS does not make errors. Thank you very much for your contribution.
@shawnchen0321 wrote:
After testing, I found that SAS does not make errors. Thank you very much for your contribution.
I think that you meant to say that the "code provided by @mkeintz does what is needed".
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.
Ready to level-up your skills? Choose your own adventure.