Hello,
I have a column of time in minutes ranging from 0 to 1440 minutes
I want to create 2 categories for this data in sas
360< Category 1 >=0 minutes and
1440< Category 2 >=360 minutes
Please advise what sas commands would I use to achieve the above result
Hi,
You can use case when expression in proc sql or If then in data step or else you can also use proc format.
/* Method 1*/
proc sql;
select *, case when 0<=time<360 then "Cat 1"
when 360<=time<1440 then "Cat 2"
else "NA" end as cat
from have;
quit;
/* Method 2*/
data want ;
set have;
if 0<=time<360 then cat="Cat 1";
else if 360<=time<1440 then cat="Cat 2";
else cat='NA';
run;
/* Method 3*/
proc format;
value timevar 0-360="cat1"
360-above="cat 2";
run;
data want;
set have;
format time_variable timevar.;
run;
Hi and welcome to the SAS community 🙂
Does your actual data contain only two categories? Or more?
If just the two, do something like this
data have;
do time=1 to 2000;
output;
end;
run;
data want;
set have;
Category=ifc((0 <= time <=360), "Category 1", "Category 2");
run;
Thankyou !!
Yes and These are the only two categories that I want to create
Ok. I have edited the above answer with a small code example.
Let me know if it fits your needs
Thankyou for your response Im new to sas therefore used if then /else as Im familiar
Will try the IFC function as well
Hi,
You can use case when expression in proc sql or If then in data step or else you can also use proc format.
/* Method 1*/
proc sql;
select *, case when 0<=time<360 then "Cat 1"
when 360<=time<1440 then "Cat 2"
else "NA" end as cat
from have;
quit;
/* Method 2*/
data want ;
set have;
if 0<=time<360 then cat="Cat 1";
else if 360<=time<1440 then cat="Cat 2";
else cat='NA';
run;
/* Method 3*/
proc format;
value timevar 0-360="cat1"
360-above="cat 2";
run;
data want;
set have;
format time_variable timevar.;
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.