DATA Step, Macro, Functions and more

Date output to Month and Year

Accepted Solution Solved
Reply
Contributor
Posts: 37
Accepted Solution

Date output to Month and Year

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;

Accepted Solutions
Solution
‎04-10-2017 07:49 PM
Contributor
Posts: 56

Re: Date output to Month and Year

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


All Replies
PROC Star
Posts: 166

Re: Date output to Month and Year

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

Solution
‎04-10-2017 07:49 PM
Contributor
Posts: 56

Re: Date output to Month and Year

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;
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

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