BookmarkSubscribeRSS Feed
Manojahuja1418
Calcite | Level 5

Hi  Team,

 

In attached file I want to categorize the respondent into three category based on their spending(below 100,100-200 and above 200

) and how we count the number of unique customer in each group.

however I have tried below codes but not successful to finding the result-

 

proc sql;
create table new as
select
case
when amtspent > 100 then "below 100"
when amtspent 100-200 then "between100 to 200"
when amtspent < 200 then "above 200"
else
value "nothing";
end;
run;

 

Kindly check and advise.

 

Thanks,

Manoj Ahuja

3 REPLIES 3
andreas_lds
Jade | Level 19

Using case for such tasks is possible, but not recommended. Using a self-defined format is the way to go. The following code uses sashelp.class and the age-variable.

proc format;
   value Age
      LOW -< 12 = 'under 12'
      12-14 = '12-14'
      14 <- HIGH = 'above 14'
   ;
run;

proc summary data=sashelp.class nway;
   class Age;
   format Age Age.;
   output out=countAgeGroup(drop=_type_ rename=(_freq_=count));
run;
ballardw
Super User

Heartily agree with @andreas_lds 

 

One of the strong features of SAS is the concept of a display format. You can do multiple types of analysis on groups based on formats without adding lots of extra variables.

 

Using a format in the SASHELP.CLASS data set we could also provide groups of over/under a single age (creating 2 groups), or 4.

With the MULTILABEL format option, though only useable directly my a few procedures such as Proc means, Tabulate and Report, you can even create nested groups.

 

Also you can apply the same format to multiple variables. So suppose you had other variables that you wanted to analyze with the same boundary values such as "planned spending" or "last year amount". Apply the same format to those variables. Otherwise you have to rewrite the same code for each variable.

 

With numeric values you can have the numeric value order control appearance in procedures. With your example the default sort order and the appearance order in many procedures would be "above 200" , "below 100" then "between 100 to 200". Which is somewhat odd in most senses.

Tom
Super User Tom
Super User

REMOVE the VALUE keyword from the ELSE clause of your CASE.

Use valid boolean logic in the WHEN clauses

case
when amtspent > 100 then "below 100"
when 100<= amtspent <=200 then "between100 to 200"
when amtspent > 200 then "above 200"
else "nothing"
end

REMOVE the extra semi-colons in the middle of your CREATE statement.

Give your new variable that the CASE is generating a name.

ADD a FROM clause so SQL knows what you are selecting from.

SQL does not need RUN statement. Each statement runs immediately. Use QUIT statement to end the PROC SQL step.

proc sql;
create table new as
  select 
    *
   ,case
      when amtspent > 100 then "below 100"
      when 100<= amtspent <=200 then "between100 to 200"
      when amtspent > 200 then "above 200"
      else "nothing"
    end as new_var
  from have
;
quit;

 

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 3 replies
  • 8679 views
  • 0 likes
  • 4 in conversation