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

Hi I am using SAS 9.4 and trying to create a new variable to include the following code

 

data work.RUCA_class;

set raw.raw_ruca18;

if RUCA30 = 1.0 1.1 2.0 2.1 3.0 4.1 5.1 7.1 8.1 10.1 then cat_A = 'Urban Focused';

if RUCA30 = 4.0 4.2 5.0 5.2 6.0 6.1 then cat_A = 'Large Rural City/Town (micropolitan) focused';

if RUCA30 = 7.0 7.2 7.3 7.4 8.0 8.2 8.3 8.4 9.0 9.1 9.2 then cat_A = 'Small Rural Town Focused';

if RUCA30 = 10.0 10.2 10.3 10.4 10.5 10.6 then cat_A = 'Isolated Small Rural Town Focused';

run;

 

When I run this I get an error expecting an arithmetic operator. Any help on why this is not working would be great. Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Instead of multiple if/then, use a select() block:

data work.RUCA_class;
set raw.raw_ruca18;
select (RUCA30);
  when ("1.0","1.1","2.0","2.1","3.0","4.1","5.1","7.1","8.1","10.1")
    cat_A = 'Urban Focused';
  when ("4.0","4.2","5.0","5.2","6.0","6.1")
    cat_A = 'Large Rural City/Town (micropolitan) focused';
  when ("7.0","7.2","7.3","7.4","8.0","8.2","8.3","8.4","9.0","9.1","9.2")
    cat_A = 'Small Rural Town Focused';
  when ("10.0","10.2","10.3","10.4","10.5","10.6")
    cat_A = 'Isolated Small Rural Town Focused';
  otherwise;
end;
run;

or contemplate using a format:

proc format;
value $ruca
  "1.0" = 'Urban Focused'
  "1.1" = 'Urban Focused'
  "2.0" = 'Urban Focused'
  "2.1" = 'Urban Focused'
  "3.0" = 'Urban Focused'
  "4.1" = 'Urban Focused'
  "5.1" = 'Urban Focused'
  "7.1" = 'Urban Focused'
  "8.1" = 'Urban Focused'
  "10.1" = 'Urban Focused'
  "4.0" = 'Large Rural City/Town (micropolitan) focused'
  /* and so on */
  OTHER = ""
;
run;

data work.RUCA_class;
set raw.raw_ruca18;
cat_A = put(RUCA30,$ruca.);
run;

If you have a lookup table for the Rural Urban Commuting Area Codes, you can automate the creation of the format.

View solution in original post

6 REPLIES 6
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Well first off we can't see your data or structure which does not help.  The first thing that jumps out at me is this:

if RUCA30 = 1.0 1.1 2.0 2.1 3.0 4.1 5.1 7.1 8.1 10.1 then cat_A = 'Urban Focused';

Numeric variables can only have one decimal point.  Therefore if this ruca30 variable is numeric, this statement is invalid.  If it is character, then you need to put quotes arround it.

 

Perhaps you mean it is a list of values something like:

if ruca30 in (1,0.1,12...)
Amir
PROC Star

Hi,

 

As well as what @RW9 has said, you might also want to consider using else if your conditions are mutually exclusive, e.g.:

 

data want;
   set sashelp.class;
   
   if age in (10,20,30) then
      flag = 1;
   else
      if age in (11,21,31) then
         flag = 2;
      else
         if age in (12,22,32) then
            flag = 3;
         else
            flag = 0;
run;

 

 

Regards,

Amir.

Astounding
PROC Star

Besides switching to the IN operator (that eliminates the error message) you will also need to add this early in the DATA step:

 

length cat_A $ 35;

 

Otherwise cat_A receives its length from the first time it is mentioned and values longer than "Urban Focused" will be truncated.

GS2
Obsidian | Level 7 GS2
Obsidian | Level 7

data work.RUCA_class;

set work.RUCA_class;

length cat_A $35;

if RUCA30 = ('1.0','1.1','2.0','2.1','3.0','4.1','5.1','7.1','8.1','10.1') then cat_A = 'Urban Focused';

if RUCA30 = ('4.0','4.2','5.0','5.2','6.0','6.1') then cat_A = 'Large Rural City/Town (micropolitan) focused';

if RUCA30 = ('7.0','7.2','7.3','7.4','8.0','8.2','8.3','8.4','9.0','9.1','9.2') then cat_A = 'Small Rural Town Focused';

if RUCA30 = ('10.0','10.2','10.3','10.4','10.5','10.6') then cat_A = 'Isolated Small Rural Town Focused';

run;

 

 

I converted the variable to a character and added the length but I am still getting the same error message. Am I not creating the variable 'cat_A' properly?

Astounding
PROC Star

Look at the examples ... no equal sign, use the word IN instead.

 

Also, if RUCA30 is a character variable, the code is otherwise correct.

 

If it's numeric, remove all the quotes around the numbers.

Kurt_Bremser
Super User

Instead of multiple if/then, use a select() block:

data work.RUCA_class;
set raw.raw_ruca18;
select (RUCA30);
  when ("1.0","1.1","2.0","2.1","3.0","4.1","5.1","7.1","8.1","10.1")
    cat_A = 'Urban Focused';
  when ("4.0","4.2","5.0","5.2","6.0","6.1")
    cat_A = 'Large Rural City/Town (micropolitan) focused';
  when ("7.0","7.2","7.3","7.4","8.0","8.2","8.3","8.4","9.0","9.1","9.2")
    cat_A = 'Small Rural Town Focused';
  when ("10.0","10.2","10.3","10.4","10.5","10.6")
    cat_A = 'Isolated Small Rural Town Focused';
  otherwise;
end;
run;

or contemplate using a format:

proc format;
value $ruca
  "1.0" = 'Urban Focused'
  "1.1" = 'Urban Focused'
  "2.0" = 'Urban Focused'
  "2.1" = 'Urban Focused'
  "3.0" = 'Urban Focused'
  "4.1" = 'Urban Focused'
  "5.1" = 'Urban Focused'
  "7.1" = 'Urban Focused'
  "8.1" = 'Urban Focused'
  "10.1" = 'Urban Focused'
  "4.0" = 'Large Rural City/Town (micropolitan) focused'
  /* and so on */
  OTHER = ""
;
run;

data work.RUCA_class;
set raw.raw_ruca18;
cat_A = put(RUCA30,$ruca.);
run;

If you have a lookup table for the Rural Urban Commuting Area Codes, you can automate the creation of the format.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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