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

## 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'

1 ACCEPTED SOLUTION

Accepted Solutions
Meteorite | Level 14

## Re: CHARACTER CONTAINS

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
3 REPLIES 3
Meteorite | Level 14

## Re: CHARACTER CONTAINS

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
Quartz | Level 8

## 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

## Re: CHARACTER CONTAINS

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;``````

Discussion stats
• 3 replies
• 17726 views
• 0 likes
• 3 in conversation