Solved
New Contributor
Posts: 2

# do loops and if then statements for numeric / character variables

Hello all,

I'm new to SAS , and I'm really tired of this problem I keep running into, but I can't figure it out.

I need to categorize data all the time with new character variables based on numeric values of another variable, e.g.:

This works fine:

if dm_prev_adj < Q1_dm then dm_prev_lvl = 'Low';

The "issue" is that it is horribly inefficient.  I have to do this for each level, for each variable.. in this instance it is only nine lines of code (3 variables, 3 levels), but I have to believe that a do loop can do this easier. When I use a do loop and a numeric value it works fine for numbers:

do i = 1 to 3;

if adj(i) <= q1(i) then lvl(i)=1;

else if q1(i) < adj(i) < q3(i) then lvl(i)=2;

else lvl(i)=3;

end;

But if I change those numeric values to words, aka 'Low' 'Medium' 'High' it bombs out and says:

NOTE: Invalid numeric data, 'Low' , at line 1451 column 32.

NOTE: Invalid numeric data, 'High' , at line 1453 column 13.

NOTE: Invalid numeric data, 'Medium' , at line 1452 column 44

over and over and over...

This seems really easy but I cannot find the answer, would appreciate any help or alternatives.

Accepted Solutions
Solution
‎07-30-2015 08:33 PM
Super User
Posts: 6,751

## Re: do loops and if then statements for numeric / character variables

When you define your array, you need to define the elements as being character.  (They will be numeric by default.)  It's as easy as this:

array lvl {3} \$ 6 lvl1-lvl3;

Good luck.

All Replies
Solution
‎07-30-2015 08:33 PM
Super User
Posts: 6,751

## Re: do loops and if then statements for numeric / character variables

When you define your array, you need to define the elements as being character.  (They will be numeric by default.)  It's as easy as this:

array lvl {3} \$ 6 lvl1-lvl3;

Good luck.

New Contributor
Posts: 2

## Re: do loops and if then statements for numeric / character variables

Thanks Astounding and to all-- I really had hoped it was something simple, but had no idea it was that simple!

Posts: 1,147

## Re: do loops and if then statements for numeric / character variables

I agree with @Astounding, alternatively you can try do over , its another way of  using arrays

array name \$ m1-m3;

do over name;

name= .....conversion code........;

end;

Thanks,

Jag

Thanks,
Jag
Valued Guide
Posts: 765

## Re: do loops and if then statements for numeric / character variables

Hi ... in addition to declaring a character array as "Astounding" has described, another way to ensure that you have a character array is to place the ARRAY statement in the data step after the SET statement.  That way, SAS already knows the variable type when you use the ARRAY statement ... assuming that the variable names match the array name (e.g. array lvl comprises lvl1 lvl2 lvl3)

data new;

set old;

array lvl(3);

array q1(3);

array q3(3);

do i = 1 to 3;

if adj(i) <= q1(i) then lvl(i)=1;

else if q1(i) < adj(i) < q3(i) then lvl(i)=2;

else lvl(i)=3;

end;

run;

Posts: 1,147

## Re: do loops and if then statements for numeric / character variables

the code of mikezeb could be modified with do over as below , please try . hope it helps

data new;

set old;

array lvl lvl1-lvl3;

array q1 _q1-_q3;

array q3 _qs1-_qs3;

do over lvl;

if adj(i) <= q1(i) then lvl(i)=1;

else if q1(i) < adj(i) < q3(i) then lvl(i)=2;

else lvl(i)=3;

end;

run;

Thanks,

Jag

Thanks,
Jag
🔒 This topic is solved and locked.