BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Sandeep77
Lapis Lazuli | Level 10

Hi all,

I am trying to categorize the customer age group using the Data step but it shows an error in the code (Statement is not valid). Could you please check and let me know what is the mistake in the code? Thanks

Data Age_group;
set Age;
if Age => 10 then 'years0-10';
else if Age => 20 then 'years11-20';
else if Age => 30 then 'years21-30';
else age = 'Others';
run;

Error Log:
29         Data Age_group;
30         set Age;
31         if Age => 10 then 'years0-10';
                             ___________
                             180
32         else if Age => 20 then 'years11-20';
                                  ____________
                                  180
33         else if Age => 30 then 'years21-30';
                                  ____________
                                  180
ERROR 180-322: Statement is not valid or it is used out of proper order.

34         else age = 'Others';
35         run;

NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).
      34:12   
NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.AGE_GROUP may be incomplete.  When this step was stopped there were 0 observations and 65 variables.
WARNING: Data set WORK.AGE_GROUP was not replaced because this step was stopped.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      user cpu time       0.00 seconds
      system cpu time     0.00 seconds
      memory              2341.31k
      OS Memory           31596.00k
2                                                          The SAS System                         09:27 Wednesday, December 14, 2022

      Timestamp           12/14/2022 01:48:40 PM
      Step Count                        71  Switch Count  0
      
1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

There are 2 main problems here.

 

  1. You're missing the age = part in a few of your lines.
  2. You have a numeric variable Age in your data and you try to define a character variable named age. This will fail. 

 

Your code should look something like this

 

Data Age_group;
set sashelp.class;
if      Age => 10 then agegroup = 'years0-10';
else if Age => 20 then agegroup = 'years11-20';
else if Age => 30 then agegroup = 'years21-30';
else                   agegroup = 'Others';
run;

View solution in original post

3 REPLIES 3
PeterClemmensen
Tourmaline | Level 20

There are 2 main problems here.

 

  1. You're missing the age = part in a few of your lines.
  2. You have a numeric variable Age in your data and you try to define a character variable named age. This will fail. 

 

Your code should look something like this

 

Data Age_group;
set sashelp.class;
if      Age => 10 then agegroup = 'years0-10';
else if Age => 20 then agegroup = 'years11-20';
else if Age => 30 then agegroup = 'years21-30';
else                   agegroup = 'Others';
run;
PaigeMiller
Diamond | Level 26

In my opinion, a superior method to handling this is custom formats.

 

proc format;
    value agef
         0-10='Years 0-10'
         11-20='Years 11-20'
         21-30='Years 21-30';
run;

 

And then assign the format to the variable in a DATA step or PROC.

 

format age agef.;

 

 

As a style issue, when you do this replacement of a numeric variable with text, using either your method or my method, make it look like proper English (or whatever language you are using), as I have done with Years 0-10 instead of years0-10 as you have done. Or even better, remove the Years from the value since everyone understands that a variable named age implies you are using years.

--
Paige Miller

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 728 views
  • 1 like
  • 3 in conversation