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

Hi All,

 

I get a dataset from a Sharepoint list and the date value is date and time.

 

I use the below to convert to a yymmdd and format as mmmyy

 

DefinedPeriod=INPUT(PUT(Date ,8.),YYMMDD8.);format DefinedPeriod monyy7.;

 

However I need to count the number of defined periods ( number of months from the dates entered)

 

Eaxample of Date entries are: 01/03/2017, 02/03/2017,01/04/2017, 02/04/2017.

 

The code below gives me a count of DefinedPeriodA of 4 when I want it to be 2.

 

Any help is appreciated.

 

by DefinedPeriod;if first. DefinedPeriod then DefinedPeriodA= 1;
1 ACCEPTED SOLUTION

Accepted Solutions
lakshmi_74
Quartz | Level 8
data have;
input defined_period DDMMYY10.;
datalines;
01/03/2017
02/03/2017
01/04/2017
02/04/2017
03/04/2017
;
run;
proc print;run;
data have;
set have;
format defined_period DDMMYY10.;
month=month(defined_period);
run;
proc sql;
select month,count(*) as total from have group by month;
quit;

View solution in original post

2 REPLIES 2
novinosrin
Tourmaline | Level 20

Assigning a month format doesn't change the stored values of your date variable that is they still remain as date values displaying with your month format. I would suggest to extract month values using month function and the group by month so that you would get your desired result:

/*sample*/

data have;
input defined_period ddmmyy10.;
format defined_period ddmmyy10.;
month=month(defined_period);
datalines;
01/03/2017
02/03/2017
01/04/2017
02/04/2017
;
proc sort data=have;
by month;
run;

data want;
set have;
by month;
if first.month then count=0;
count+1;
if last.month;
run;

 

Regards,

Naveen Srinivasan

lakshmi_74
Quartz | Level 8
data have;
input defined_period DDMMYY10.;
datalines;
01/03/2017
02/03/2017
01/04/2017
02/04/2017
03/04/2017
;
run;
proc print;run;
data have;
set have;
format defined_period DDMMYY10.;
month=month(defined_period);
run;
proc sql;
select month,count(*) as total from have group by month;
quit;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

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
  • 2 replies
  • 1083 views
  • 1 like
  • 3 in conversation