BookmarkSubscribeRSS Feed
na_sas
Calcite | Level 5

Hi everyone! 

I wanted to create season variable from months but I get error or I just have Winter or Fall:

I'd appreciate if you'd help me ... Thanks!

data a.hospital1;
set a.hospital;
length Season $10;
if Month in ('March','April','May') then Season='Spring';
else if Month in ('June','July','August') then Season='Summer';
else if Month in ('September','October','November') then Season='Fall';
else Season='Winter';
run;

 

I've tried this one too:


data a.hospital1;
set a.hospital;
length Season $10;
if (month='January') then season='Winter';
else if (month='February') then season='Winter';
else if (month='March') then season='Winter';
else if (month='April') then season='Spring';
else if (month='May') then season='Spring';
else if (month='June') then season='Spring';
else if (month='July') then season='Summer';
else if (month='August') then season='Summer';
else if (month='September') then season='Summer';
else if (month='October') then season='Fall';
else if (month='November') then season='Fall';
else season='Fall';
run;

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

Welcome to the SAS Communities 🙂

 

Here is a format approach

 

proc format lib=work;
   value $season 'December', 'January', 'February'  = 'Winter'
                 'March', 'April', 'May'            = 'Spring'
                 'June', 'July', 'August'           = 'Summer'
                 'September', 'October', 'November' = 'Fall'
                 other                              = 'N/A'
;
run;

data have;
input Month $20.;
datalines;
January
February
March
April
May
June
July
August
September
October
November
December
NotAMonth
;

data want;
   set have;
   Season=put(Month, $Season.);
run;

proc print data=want;
run;
VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

In you first data step, I would not use this line as my last else statement.

else Season='Winter';

in your second data step the same thing is happening but you set Season = "Fall";

if your records have data like "DECEMBER", "Dec", "MARCH", "MAR: they would all be set to Winter.

 

use the last else statement to catch any data that has errors based on the prior statement. 

like this

else Season = 'Error';

 

Does month always have mixed case data?

maybe upcase the test like this 

if upcase(substr(month,1,3)) in ('JAN','FEB',MAR') then Season  = "Winter";

 

You also have winter defined as containing defernent months in the 2 data steps.

 

PGStats
Opal | Level 21

Taking a few precautions should help:

 

data a.hospital1;
set a.hospital;
length Season $10;
select (propcase(month));
    when ('March','April','May') Season='Spring';
    when ('June','July','August') Season='Summer';
    when ('September','October','November') Season='Fall';
    when ('December','January','February') Season='Winter';
    otherwise Season='Error';
    end;
run;
PG

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 2112 views
  • 0 likes
  • 5 in conversation