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

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

1 ACCEPTED SOLUTION

Accepted Solutions
SuryaKiran
Meteorite | Level 14

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;
Thanks,
Suryakiran

View solution in original post

6 REPLIES 6
PeterClemmensen
Tourmaline | Level 20

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;
Ranjeeta
Pyrite | Level 9

Thankyou !!

Yes and These are the only two categories that I want to create

PeterClemmensen
Tourmaline | Level 20

Ok. I have edited the above answer with a small code example. 

 

Let me know if it fits your needs 

Ranjeeta
Pyrite | Level 9

Thankyou for your response  Im new to sas therefore used if then /else as Im familiar 

Will try the IFC function as well

SuryaKiran
Meteorite | Level 14

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;
Thanks,
Suryakiran
Ranjeeta
Pyrite | Level 9
Thankyou very much this is very helpful

I used method 2




SAS Innovate 2025: Call for Content

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!

Submit your idea!

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
  • 6 replies
  • 5832 views
  • 2 likes
  • 3 in conversation