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.
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;
data datasetB;
set datasetA;
if not index(ACCT_NO,'-') ;
ACCTKEY = input(ACCT_NO,best.);
run;
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;
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.
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;
For 'index' can I check more than 1 strings?
for example, can I do
if not index(ACCT_NO, '-') and not index(ACCT_NO, '/')?
Yes indeed. Now you are writing a compund expression wth logical operators NOT, AND 🙂
It would have been faster to test than to ask.
Yes you can.
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;
The data step is not going to recognize LIKE as an operator. That only works in PROC SQL code or in WHERE statements.
I see. Thanks!
Also:
data datasetB;
set datasetA;
where ACCT_NO not like '%-%' ;
The like operator is not as efficient as the index function though.
I am gonna use 'index' now. But thank you!
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.