Date format

Accepted Solution Solved
Reply
Contributor
Posts: 26
Accepted Solution

Date format

I am pretty new on SAS and I am not getting to create a new variable in SAS based on a date variable with the format 

monname9. 

I want to add the year season for each month in the monname9. date, for example, I want to create a variable called Year Season for December, January and February I want to put "winter"

March, April and May I want to call "spring"

June, July and August call "summer"

and the other 3 months "fall"

this should repeat for some years in my data.

 

Any thoughts how I could do that?

Thanks


Accepted Solutions
Solution
‎10-31-2017 09:42 AM
Super User
Posts: 21,546

Re: Date format


Giovani wrote:

 

I am looking to create a new variable matching each line in the variable Month with a corresponding line in the  new variable Season with a label of winter, fall, summer, or spring.

Thanks

 


Did you try applying the format created? See the full example below.

*Create a format;
data ctrl;
  fmtname='Seasons';
  format start end date9.;
  do i=0 by 1;
    start =intnx('month','01jan1960'd,i*3,'b');
    end   =intnx('month',start,2,'e');
    select(month(start));
      when(12,1,2)  label='Winter';
      when(3,4,5)   label='Spring';
      when(6,7,8)   label='Summer';
      when(9,10,11) label='Fall';
      otherwise;
    end;
    output;
    if start > intnx('year',today(),5,'e') then leave;
  end;
run;

proc format cntlin=ctrl;
run;

*demo application of format;
data want;
    format date date9. date_formatted seasons.;

    do date='01Jan2016'd to '31Dec2017'd;
        date_formatted = date;
        date_char =put(date, seasons.);
        output;
    end;
run;

View solution in original post


All Replies
Super User
Posts: 12,148

Re: Date format

Is this supposed to have the YEAR value as part of the result or not?

I.E Dec 2016 = "Winter" or Dec 2016= "Winter 2016"?

And if year is part of the result would Dec 2016, Jan 2017 and Feb 2017 all be "Winter 2016", "Winter 2017" or the year per calendar month?

Contributor
Posts: 26

Re: Date format

Hi,

Thanks for the reply. Just the season "winter" is the expected result.

Super User
Posts: 21,546

Re: Date format

Use a FORMAT. 

 


Giovani wrote:

I am pretty new on SAS and I am not getting to create a new variable in SAS based on a date variable with the format 

monname9. 

I want to add the year season for each month in the monname9. date, for example, I want to create a variable called Year Season for December, January and February I want to put "winter"

March, April and May I want to call "spring"

June, July and August call "summer"

and the other 3 months "fall"

this should repeat for some years in my data.

 

Any thoughts how I could do that?

Thanks


 

Respected Advisor
Posts: 4,284

Re: Date format

[ Edited ]

Here some code demonstrating how you could create a format "Season." for use with your data.

data ctrl;
  fmtname='Seasons';
  format start end date9.;
  do i=0 by 1;
    start =intnx('month','01jan1960'd,i*3,'b');
    end   =intnx('month',start,2,'e');
    select(month(start));
      when(12,1,2)  label='Winter';
      when(3,4,5)   label='Spring';
      when(6,7,8)   label='Summer';
      when(9,10,11) label='Fall';
      otherwise;
    end;
    output;
    if start > intnx('year',today(),5,'e') then leave;
  end;
run;

proc format cntlin=ctrl;
run;

 

Documented here:

http://documentation.sas.com/?docsetId=proc&docsetTarget=n1e19y6lrektafn1kj6nbvhus59w.htm&docsetVers...

Contributor
Posts: 26

Re: Date format

Thanks for the reply!

I went true the codes and this created all the years season from 1960 until today, but that is not what I was expecting.

I do have a variable Month with many observations starting in 2003 going until nowadays. The variable month has a format MONNAME9. 

I am looking to create a new variable matching each line in the variable Month with a corresponding line in the  new variable Season with a label of winter, fall, summer, or spring.

Thanks

 

Solution
‎10-31-2017 09:42 AM
Super User
Posts: 21,546

Re: Date format


Giovani wrote:

 

I am looking to create a new variable matching each line in the variable Month with a corresponding line in the  new variable Season with a label of winter, fall, summer, or spring.

Thanks

 


Did you try applying the format created? See the full example below.

*Create a format;
data ctrl;
  fmtname='Seasons';
  format start end date9.;
  do i=0 by 1;
    start =intnx('month','01jan1960'd,i*3,'b');
    end   =intnx('month',start,2,'e');
    select(month(start));
      when(12,1,2)  label='Winter';
      when(3,4,5)   label='Spring';
      when(6,7,8)   label='Summer';
      when(9,10,11) label='Fall';
      otherwise;
    end;
    output;
    if start > intnx('year',today(),5,'e') then leave;
  end;
run;

proc format cntlin=ctrl;
run;

*demo application of format;
data want;
    format date date9. date_formatted seasons.;

    do date='01Jan2016'd to '31Dec2017'd;
        date_formatted = date;
        date_char =put(date, seasons.);
        output;
    end;
run;
☑ This topic is solved.

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

Discussion stats
  • 6 replies
  • 202 views
  • 0 likes
  • 4 in conversation