SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Ameurgen
Obsidian | Level 7

hello every body;

i need some help  with my sas code, in fact, i alnalsye some data, with records and dates as variables.

So my question is simple how can i create a year/season  varaible to my records , my approach is to use months to refer to season e.g. 12, 1 . 2 for 'winter' and 3,4, 5 for spring. but the problem is only in winter because one month is not in same year e.g. 2014 Dec  is not 2015 winter as Jan. 

So please can any one help  me how can i solve this .

Thank you

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data example;
  do year= 2013 to 2015;
  do month=1 to 12;
     date=mdy(month,1,year);
     output;
  end;
  end;
  format date date9.;
run;



proc format;
value fmt
12,1,2='winter'
3,4,5 ='spring'
6,7,8 ='summer'
9,10,11='autumn'
;
run;
data have;
 set example;
 want=cats(year(intnx('year.12',date,1)),'/',put(month(date),fmt.));
run;

View solution in original post

7 REPLIES 7
ballardw
Super User

There are some questions you may need to answer. Do want all December to lumped into "Winter" or do you want Dec 2013 as "Winter 2013" and Dec 2014 as "Winter 2014" (or possibly the year as 2014/2015 respectively)?

 

One way would be to make a custom Format but need to know exactly what you want to display to write one. It isn't that hard to create such as set. The set Mydatecntl is used to make a format named my season that has boundaries starting Dec2000 running for 3 months until the start to 2050. It provides start and end of periods so any date value in the range can have the formatted text used. This is creating YYYY <season> so the year groups together. If you want Dec to be in the following calendar year for Winter use "year(start)+1" in the When(12) clause of the select. The last data step creates some dates and to display use of the format.

data mydatecntl;
   fmtname='MySeason';
   type='N';
   start='01DEC2000'd;
   length label $ 15.;
   do until (start ge '01Jan2050'd) ;
      end=intnx('month',start,2,'e');
      select (month(start));
         when (12) label= catx(' ',year(start),'Winter');
         when ( 3) label= catx(' ',year(start),'Spring');
         when ( 6) label= catx(' ',year(start),'Summer');
         when ( 9) label= catx(' ',year(start),'Fall');
         otherwise;
      end;
      output;
      start=intnx('month',start,3,'b');

   end;
run;

proc format cntlin=   mydatecntl;
run;

data example;
  do year= 2013 to 2015;
  do month=1 to 12;
     date=mdy(month,1,year);
     output;
  end;
  end;
  format date Myseason.;
run;

Groups that are created by formats are honored in analysis and reporting procedures and generally for graphing procedures. The exceptions are addressed as needed.

 

Ameurgen
Obsidian | Level 7

 

hello  sir, 

Thank you for your quick reply also the explaination  kinda very useful but , i think the problem remain always, the creation season is easy for me but my onlu problem here is , season definition of winter is start from 12 (previous year) to Jan. and Fab (2 ) for the following year , what ever the year is, i need to tell sas to take this information as important because i will use year/ season as  concatenate varaible in my model. 

Thank you again.

 

Regards

Ksharp
Super User
data example;
  do year= 2013 to 2015;
  do month=1 to 12;
     date=mdy(month,1,year);
     output;
  end;
  end;
  format date date9.;
run;



proc format;
value fmt
12,1,2='winter'
3,4,5 ='spring'
6,7,8 ='summer'
9,10,11='autumn'
;
run;
data have;
 set example;
 want=cats(year(intnx('year.12',date,1)),'/',put(month(date),fmt.));
run;
Ameurgen
Obsidian | Level 7
thank you sir,
Its work, now,

regards
ErikLund_Jensen
Rhodochrosite | Level 12

Hi @Ameurgen 

 

You can make the Season variable as tekst like this:

 

data have;
format date DATE9.;
  Date = '03oct2021'd; output;
  Date = '12dec2021'd; output;
  Date = '27jan2022'd; output;
  Date = '04may2022'd; output;
  Date = '18jul2022'd; output;
run;

data want;
  set have;
  length Season $12;
  select;
    when (month(Date) in(12, 1, 2)) Season  = 'Winter';
    when (month(Date) in(3, 4, 5)) Season  = 'Spring';
    when (month(Date) in(6, 7, 8)) Season  = 'Summer';
    when (month(Date) in(9, 10, 11)) Season  = 'Fall';
    otherwise Season = 'n.a.';
  end;
  Season = catx(' ', put(year(Date),4.), Season);
run;

The result is:

 

season.gif

 

 

Ameurgen
Obsidian | Level 7

Thank you sir for your contribution, 

but i think the same problem remain, in winter , as i said, winter season is note like the others seasons by definition he begain in dec in previous year so its difficult for me to set dec month from 1st year to next year.

**its mentionned in youyr results.

Thank you again

ErikLund_Jensen
Rhodochrosite | Level 12

Hi @Ameurgen 

Oh, I understand your answer to mean that you want the year shifted one month, so december is part of the winter season in the following year. You could easily obtain that with my code (change marked with red text😞

 

data want;
  set have;
  length Season $12;
  select;
    when (month(Date) in(12, 1, 2)) Season  = 'Winter';
    when (month(Date) in(3, 4, 5)) Season  = 'Spring';
    when (month(Date) in(6, 7, 8)) Season  = 'Summer';
    when (month(Date) in(9, 10, 11)) Season  = 'Fall';
    otherwise Season = 'n.a.';
  end;
  Season = catx(' ', put(year(Date)+(month(date)=12),4.), Season);
run;

 

 

 

 

 

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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