DATA Step, Macro, Functions and more

scan funtion: scan number 9 and 10

Reply
Super Contributor
Posts: 336

scan funtion: scan number 9 and 10

I need to create a new variable, with scan

 

if scan  var1, var2, var3 , those variables are alpha numeric characters, if there is number 9, then newvar=9;

if there is a number 10, then newvar=10

it seems to me difficult, since 10 is a two digit and 9 is one digit

would you please give advice

 

the var looks like

abc abcde9

abc abc.cbc9_dd_ee12

 

thanks

Super Contributor
Posts: 255

Re: scan funtion: scan number 9 and 10

 
Super Contributor
Posts: 255

Re: scan funtion: scan number 9 and 10

Does this help you?

data have;
array k[*] $ newvar1 -newvar3;

infile datalines missover;
input v1 $ v2$ v3$;
array var[*]$ v1 - v3;
do _N_ = 1 to dim(var);
   k[_N_] = compress(var[_N_], , 'kd');
end;
datalines;
abc abcde9          
abc abc.cbc9 _dd_ee12
;
run;

proc print data = have;
run;
Super User
Posts: 5,085

Re: scan funtion: scan number 9 and 10

You have omitted too much.

 

Why are we scanning for alphnumeric characters?  If we find ????9#$@% should that not be a "9"?

 

Evidently, a decimal point is permitted as an alphanumeric character.  What else would be permitted?  Underscores?

 

Does finding a "9" include either of these:  abc29 or xyz92

 

What should happen if we find both a "9" and a "10" such as "ab9 abcd10"?

 

It would cut down on a lot of the work if you would provide more of the details.

Super Contributor
Posts: 336

Re: scan funtion: scan number 9 and 10

[ Edited ]

Thank you very much,

the values are only

 

letters, with number 9 or 10, or 2, so I need to use scan to find whether it is 9 or 10, if it is 9, then it will labeled as "9", it will labeled as "10" if it includes "10"

 

the value is just like:

 

"from iab2.iab10_ab_fi12"

 

or

 

" from iab2.iab9_ab_fi15"

 

And I only care the middle whether it is "9" or "10, but the last numbers are not "9" or "10", it is either "12" or "15"

 

Super User
Posts: 5,085

Re: scan funtion: scan number 9 and 10

OK, here's something you can try to see if it meets your requirements:

 

if index(var, '10') then value='10';

else if index(var, '9') then value='9';

 

It sounds like it should be correct, but you will have to test against a variety of cases to see if you get the results you want.

Ask a Question
Discussion stats
  • 5 replies
  • 257 views
  • 0 likes
  • 3 in conversation