BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Tekkers
Calcite | Level 5

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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.

View solution in original post

5 REPLIES 5
Astounding
PROC Star

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.

Tekkers
Calcite | Level 5

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

Jagadishkatam
Amethyst | Level 16

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
MikeZdeb
Rhodochrosite | Level 12

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;

Jagadishkatam
Amethyst | Level 16

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

Thanks,
Jag

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 1954 views
  • 6 likes
  • 4 in conversation