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

*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
SuryaKiran
Meteorite | Level 14

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

View solution in original post

3 REPLIES 3
SuryaKiran
Meteorite | Level 14

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
rvsidhu035
Quartz | Level 8
DATA GIVEN;
INPUT DAT$ @@;
if FINDC(DAT,'XYZ') THEN DESTINATION='INTERNATIONAL';
ELSE DESTINATION='DOMESTIC';
DATALINES;
T123 TY333 1357Z UZYX 888 ABC
;RUN;
Tom
Super User Tom
Super User

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;

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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