BookmarkSubscribeRSS Feed
Bal23
Lapis Lazuli | Level 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

5 REPLIES 5
KachiM
Rhodochrosite | Level 12
 
KachiM
Rhodochrosite | Level 12

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;
Astounding
PROC Star

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.

Bal23
Lapis Lazuli | Level 10

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"

 

Astounding
PROC Star

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.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 5 replies
  • 1065 views
  • 0 likes
  • 3 in conversation