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 2025: Register Today!

 

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 2 replies
  • 1378 views
  • 1 like
  • 3 in conversation