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

Hi.. I have a dataset where a variable called ACCT_NO (character).. In there, some of the records have ACCT_NO contain '-' and some don't.. My goal is to remove those have '-' in their ACCT_NO and turn the rest's ACCT_NO into numeric.. 

 

Here is my code but I got the error message.. Anyone know what the error is? Thanks!

 

 

data datasetB;
	set datasetA;
	if ACCT_NO like '%-%' then delete;
	ACCTKEY = input(ACCT_NO,19.);
run;

8548   data datasetB;
8549       set datasetA;
8550       if ACCT_NO like '%-%' then delete;
                      ----
                      388
                      76
ERROR 388-185: Expecting an arithmetic operator.

ERROR 76-322: Syntax error, statement will be ignored.

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

Test this

 

data have;
input ACCT_NO :$10.;
cards;
123-678
4567
4657-78
57567
;

data want;
	set have;
	if not index(ACCT_NO,'-') ;
	ACCTKEY = input(ACCT_NO,best.);
run;

 

View solution in original post

12 REPLIES 12
novinosrin
Tourmaline | Level 20
data datasetB;
	set datasetA;
	if not index(ACCT_NO,'-') ;
	ACCTKEY = input(ACCT_NO,best.);
run;
newboy1218
Quartz | Level 8

But doesn't INDEX function return the position of a string? In that case, shouldn't I use this instead?

 

if index(ACCT_NO,'-') = 0;
novinosrin
Tourmaline | Level 20

yes you can which is the equivalent of NOT logical operatior(=0)


@newboy1218 wrote:

But doesn't INDEX function return the position of a string? In that case, shouldn't I use this instead?

 

if index(ACCT_NO,'-') = 0;


perator
. Your style is just as good.

novinosrin
Tourmaline | Level 20

Test this

 

data have;
input ACCT_NO :$10.;
cards;
123-678
4567
4657-78
57567
;

data want;
	set have;
	if not index(ACCT_NO,'-') ;
	ACCTKEY = input(ACCT_NO,best.);
run;

 

newboy1218
Quartz | Level 8

For 'index' can I check more than 1 strings?

 

for example, can I do

if not index(ACCT_NO, '-') and not index(ACCT_NO, '/')?
novinosrin
Tourmaline | Level 20

Yes indeed. Now you are writing a compund expression wth logical operators NOT, AND 🙂

ChrisNZ
Tourmaline | Level 20

It would have been faster to test than to ask.

Yes you can.

novinosrin
Tourmaline | Level 20

If your requirement is below unlike your original, 

 

Try indexC

 

For Example

data have;
input ACCT_NO :$10.;
cards;
123-678
4567
4657-/78
57567
;

data want;
	set have;
	if  indexc(ACCT_NO,'-/')=0 ;
	ACCTKEY = input(ACCT_NO,best.);
run;

 

 

Tom
Super User Tom
Super User

The data step is not going to recognize LIKE as an operator.  That only works in PROC SQL code or in WHERE statements.

newboy1218
Quartz | Level 8

I see. Thanks!

ChrisNZ
Tourmaline | Level 20

Also:

data datasetB;
  set datasetA;
  where ACCT_NO not like '%-%' ;

The like operator is not as efficient as the index function though.

 

 

 

 

 

newboy1218
Quartz | Level 8

I am gonna use 'index' now. But thank you!

SAS Innovate 2025: Register Now

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!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 12 replies
  • 1521 views
  • 7 likes
  • 4 in conversation