*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'
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;
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;
DATA GIVEN;
INPUT DAT$ @@;
if FINDC(DAT,'XYZ') THEN DESTINATION='INTERNATIONAL';
ELSE DESTINATION='DOMESTIC';
DATALINES;
T123 TY333 1357Z UZYX 888 ABC
;RUN;
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;
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!
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.