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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

@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

6 REPLIES 6
ballardw
Super User

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?

Giovani
Obsidian | Level 7

Hi,

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

Reeza
Super User

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


 

Patrick
Opal | Level 21

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...

Giovani
Obsidian | Level 7

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

 

Reeza
Super User

@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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 6 replies
  • 1465 views
  • 0 likes
  • 4 in conversation