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.
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.
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.
Thanks Astounding and to all-- I really had hoped it was something simple, but had no idea it was that simple!
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
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 adj(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;
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 adj adj1 - adj3;
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
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.