BookmarkSubscribeRSS Feed
vjm
Calcite | Level 5 vjm
Calcite | Level 5

HI I am trying to use the scan function . 

 

what i am trying to do is to if address 1 contains the apt number flip and change address 1 and address 2. if it doesnt keep the same values. But the code for some reason is assigning only the first character to the variable a1.

 

Example if address1= 123 main st  a1= 1. 

 

but if i flip the if and else statements it works.

 

Example if address1= 123 main st  a1= 123 main st. 

 

I do not understand why is this happening.

 

The same logic works on a diff data set.  The current address data is being imported from a excel file.

data_1;
set data _2;

if SCAN(address1,1,' ') in
('APT',) then do;
a1 = Address2;
a2 = Address1;
switch=1;
end;

else if  SCAN(address1,1,' ') not in
('APT',) then do;
a1 = Address1;
a2 = Address2;
switch=1;
end;

run;
3 REPLIES 3
Tom
Super User Tom
Super User

Not sure why you attached a FILE to show three lines of text.

209 SW KESTOR DR
14638 TROPICAL DR
7082 CEDARHURST DR

None of those lines contain the text string APT.

ballardw
Super User

Try posting data as a data step.

 

Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.

 

Make sure that the posted data has examples of both sorts of values. Your CSV didn't have any APT or records with Address2 values.

 

BTW, this has several errors.

data_1;
set data _2;

a data step starts with the keyword DATA and if you want to name the date set place it after the key word:

Data _1;

However the SET statement just takes the name of the data set.

So you either want

data _1;
set _2;

or

data _1;
set data_2;

Though I would typically expect the lower numbered of series of data sets on the SET statement.

When you get unexpected results it is also a good idea to copy the log with the code and any messages and paste that to a code box.

ChrisNZ
Tourmaline | Level 20

1. The code you supplied is invalid.

2. You logic can be simplified to:

  if ADDRESS1 =: 'APT ' then do;
    A1 = ADDRESS2;
    A2 = ADDRESS1;
    SWITCH=1;
  end;
  else do;
    A1 = ADDRESS1;
    A2 = ADDRESS2;
    SWITCH=1;
  end;

provided ADDRESS1 is left-aligned.

3. Note that variable SWITCH is always set to 1.

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 588 views
  • 0 likes
  • 4 in conversation