Solved
Contributor
Posts: 56

# CHARACTER CONTAINS

*33.I have a variable with the name of TAN_NUMBER and below are the values:
Write a program to create a new variable DESTINATION when X,Y,Z encounters the
value should be INTERNATIONAL otherwise DOMESTIC.;
T123 TY333 1357Z UZYX 888 ABC

DATA GIVEN;
INPUT DAT\$ @@;
DO I=1 TO LENGTH(DAT);
IF SUBSTR(DAT,I,1) IN ('X','Y','Z') THEN DESTINATION='INTERNATIONAL';
ELSE DESTINATION='DOMESTIC';OUTPUT;END;
DATALINES;
T123 TY333 1357Z UZYX 888 ABC
;RUN;

I WILL GET MULTIPLE OUPUT IF  X OR Y OR Z CONTAINS DESTI='INTER'

ELSE 'DOMESTIC'

Accepted Solutions
Solution
‎04-03-2018 04:32 AM
Valued Guide
Posts: 559

## Re: CHARACTER CONTAINS

[ Edited ]

Your do loop is bringing you multiple outputs. Just use INDEX or FIND or PRXMATCH function to find a specific character in your string.

`DATA GIVEN;infile datalines;INPUT DAT \$ 1-50;if prxmatch("m/X|Y|Z/oi",DAT) then DESTINATION='INTERNATIONAL';ELSE DESTINATION='DOMESTIC';DATALINES;T123 TY333 1357Z UZYX 888 ABC;RUN;DATA GIVEN;infile datalines;INPUT DAT \$ 1-50;if find(dat,'X') or find(dat,'Y') or find(dat,'Z') then DESTINATION='INTERNATIONAL';ELSE DESTINATION='DOMESTIC';DATALINES;T123 TY333 1357Z UZYX 888 ABC;RUN;DATA GIVEN;infile datalines;INPUT DAT \$ 1-50;if index(dat,'X') or index(dat,'Y') or index(dat,'Z') then DESTINATION='INTERNATIONAL';ELSE DESTINATION='DOMESTIC';DATALINES;T123 TY333 1357Z UZYX 888 ABC;RUN;`
Thanks,
Suryakiran

All Replies
Solution
‎04-03-2018 04:32 AM
Valued Guide
Posts: 559

## Re: CHARACTER CONTAINS

[ Edited ]

Your do loop is bringing you multiple outputs. Just use INDEX or FIND or PRXMATCH function to find a specific character in your string.

`DATA GIVEN;infile datalines;INPUT DAT \$ 1-50;if prxmatch("m/X|Y|Z/oi",DAT) then DESTINATION='INTERNATIONAL';ELSE DESTINATION='DOMESTIC';DATALINES;T123 TY333 1357Z UZYX 888 ABC;RUN;DATA GIVEN;infile datalines;INPUT DAT \$ 1-50;if find(dat,'X') or find(dat,'Y') or find(dat,'Z') then DESTINATION='INTERNATIONAL';ELSE DESTINATION='DOMESTIC';DATALINES;T123 TY333 1357Z UZYX 888 ABC;RUN;DATA GIVEN;infile datalines;INPUT DAT \$ 1-50;if index(dat,'X') or index(dat,'Y') or index(dat,'Z') then DESTINATION='INTERNATIONAL';ELSE DESTINATION='DOMESTIC';DATALINES;T123 TY333 1357Z UZYX 888 ABC;RUN;`
Thanks,
Suryakiran
Contributor
Posts: 56

## Re: CHARACTER CONTAINS

``````DATA GIVEN;
INPUT DAT\$ @@;
if FINDC(DAT,'XYZ') THEN DESTINATION='INTERNATIONAL';
ELSE DESTINATION='DOMESTIC';
DATALINES;
T123 TY333 1357Z UZYX 888 ABC
;RUN;``````
Super User
Posts: 7,932

## Re: CHARACTER CONTAINS

[ Edited ]

This is the wrong approach for this problem as there is a function, INDEXC(), that does what you need already.

But the reason you are getting multiple outputs is a logic problem with how you structured your DO loop.  You should not have an OUTPUT statement in the loop.  Instead you should use the loop to set the value.  Typically you set the initial value and then when you find a match change the value.  Note that DO statement allows you to have both an iterative part and a other parts.  So you can stop searching once you have found changed the destination value.

So you could construct your DESTINATION variable like this:

``````length destination \$15;
destination='DOMESTIC';
do I=1 to length(dat) while (destination='DOMESTIC');
if substr(dat,I,1) in ('X','Y','Z') then destination='INTERNATIONAL';
end;``````

☑ This topic is solved.