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
@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;
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?
Hi,
Thanks for the reply. Just the season "winter" is the expected result.
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
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:
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
@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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: