Hi
I'm a new SAS 9.4 user, trying to compute a new column using a IF-THEN statement. The below table is my weather data, the column I am trying to compute is season, for example under the column name Season; from the 1st of Sep 2017 to 30th of November 2017 from the date column will also appear under Season as Spring17.
Below is an unsuccessful attempt.
Like this:
if '01jan2020'd < DATE < '31mar2020'd then SEASON='Winter2020';
Like this:
if '01jan2020'd < DATE < '31mar2020'd then SEASON='Winter2020';
This worked thank you.
I just had to add an = after both < to include both the first and last date in that season.
Thanks again, great help.
Alternative way to assign value to season is by checking the month:
data want;
set have;
length season $8;
mm = month(date);
if 03 le mm le 05 then season = 'Automn'; else
if 06 le mm le 08 then season = 'Winter' ; else
if 09 le mm le 11 then season = 'Spring'; else
if mm in (01 02 12) then season = 'Summer';
season = cat(season, put(date,year2.));
run;
I like this solution, but the trouble is I have three different years and need to be able to identify them in season also. By checking the month only I will have three summers etc. Can I specify year also?
@EM_G wrote:
I like this solution, but the trouble is I have three different years and need to be able to identify them in season also. By checking the month only I will have three summers etc. Can I specify year also?
You missed the line where I concatenate the year to the season derived from the month format:
season = cat(season, put(date,year2.));
and, of course, you can replace the year2. format with year4. format, if preferd.
Thus you are free to work with any year and any number of years.
My apologies, I didn't realize how that worked. I understand now and this works also, so thank you.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.
Ready to level-up your skills? Choose your own adventure.