DATA Step, Macro, Functions and more

Scanning and getting combinations from a long field

Accepted Solution Solved
Reply
Contributor
Posts: 55
Accepted Solution

Scanning and getting combinations from a long field

 

I have this dataset (have) with number combinations in column b. 

If column b contains the numbers 100, 200, 100 and 200, it should give the tag ="check". 

If column b contains a combination of 100 and not 200, it should give no tag.

 

Can this be possible in one data step? 

 

Code is below:


data have;
input a b :$32.;
cards;
1 100-100
2 300
3 200-100
4 300-300-300-100
5 200
;
run;

 

/*If b in ('100', '200') then tag = "check"*/

data want;
input a b :$32. tag $5.;
cards;
1 100-100 tag
2 300
3 200-100 tag
4 300-300-100
5 200 tag
;
run;


Accepted Solutions
Solution
‎06-28-2016 08:54 AM
Super User
Posts: 7,832

Re: Scanning and getting combinations from a long field

Posted in reply to angeliquec
data have;
input a b :$32.;
cards;
1 100-100
2 300
3 200-100
4 300-300-300-100
5 200
;
run;

data want;
set have;
marker_100 = 0;
marker_else = 0;
do i = 1 to countw(b,'-');
  if scan(b,i,'-') in ('100','200')
  then marker_100 = 1;
  else marker_else = 1;
end;
if marker_100 and not marker_else then tag = 'tag';
drop i marker_100 marker_else;
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Solution
‎06-28-2016 08:54 AM
Super User
Posts: 7,832

Re: Scanning and getting combinations from a long field

Posted in reply to angeliquec
data have;
input a b :$32.;
cards;
1 100-100
2 300
3 200-100
4 300-300-300-100
5 200
;
run;

data want;
set have;
marker_100 = 0;
marker_else = 0;
do i = 1 to countw(b,'-');
  if scan(b,i,'-') in ('100','200')
  then marker_100 = 1;
  else marker_else = 1;
end;
if marker_100 and not marker_else then tag = 'tag';
drop i marker_100 marker_else;
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 55

Re: Scanning and getting combinations from a long field

Posted in reply to KurtBremser
thank you Kurt!
☑ This topic is solved.

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

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