BookmarkSubscribeRSS Feed
Vaibhav_Kalra
Calcite | Level 5

Hi

I have got a data with 20 columns and i want categorize the data using Proc SQL as per below

 

If Amount spent is less than 100 than "A"

If Amount spent is between 100 and 200 than "B"

If Amount spent is greater than 200 than "C"

 

Thanks

 

3 REPLIES 3
Oligolas
Barite | Level 11

Hi,

 

use the case Expression as explained here

 

________________________

- Cheers -

Kurt_Bremser
Super User
proc sql;
create table want as
select
  a.*,
  case
    when amount < 100 then 'A'
    when 100 <= amount <= 200 then 'B'
    when amount > 200 then 'C'
  end as category
from have a;
quit;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Please post test data in the form of a datastep:

https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat...

 

And what you want to see out at the end, otherwise we are guessing on both.  You say you have 20 columns, but never mention what these are, how they are used or any relevance to the code.  You state using SQL, but do not give any reasons for this - SQL is built to run on normalised data - i.e. few columns, lots of observations - and as such isn't a good tool for lots of columns.  Datastep and an array would be easier - obviously  still guessing as no information provided.  Its likely that you could just format the value with a custom format and avoid all conditional programming at all.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 951 views
  • 0 likes
  • 4 in conversation