Hi,
I'm very much a beginner in SAS (version 9.3), so I'm trying to handle my data the easiest way possible. I'm trying to create seasons for my data that has dates over 5 years (I have 94230 obs). First I created a column with months and then an IF THEN statement, but it only returns an empty column. So, instead I took the really loong way of doing it and went thru all my dates and created a new IF THEN statement. Now SAS reads all the dates and turn them in to seasons, except for my last row, where it only reads the first date, turn that into the right season (date 20728 to season 3), but stops reading the following dates. What to do?
First I tried this, the month column is alright but the season didn't work
data sintid2;
set sintid1;
month=kalvdat;
format month month.;
if 1>= month=<4 then season=1;
if 5>= month=<9 then season=2;
if 10>= month=<12 then season=3;
run;
Then I did this
data sintid2;
set sintid1;
if 18993>= kalvdat=<19113 then season=1;
else if 19114>= kalvdat=<19266 then season=2;
else if 19267>= kalvdat=<19358 then season=3;
else if 19359>= kalvdat=<19478 then season=1;
else if 19479>= kalvdat=<19631 then season=2;
else if 19632>= kalvdat=<19723 then season=3;
else if 19724>= kalvdat=<19843 then season=1;
else if 19844>= kalvdat=<19996 then season=2;
else if 19997>= kalvdat=<20088 then season=3;
else if 20089>= kalvdat=<20208 then season=1;
else if 20209>= kalvdat=<20361 then season=2;
else if 20362>= kalvdat=<20453 then season=3;
else if 20454>= kalvdat=<20574 then season=1;
else if 20575>= kalvdat=<20727 then season=2;
else if 20728>= kalvdat=<20819 then season=3;
run;
And as explained, it returns season for all rows except the last row (stops after 20728).
I also tried this for the last row
else if kalvdat=<20728 then season=3;
run;
But then, another strange thing happends: then the row above 20575-20727 doesn't work, but all days in the last row (day 20728-20819) get season 3.
I assume I am missing out on some small command, but which?
Thank you,
Kind Regards
Lisa
What you are missing is that the format does not change the actual value of a variable, only how it is displayed.
What you want to do is probably something like this (using the month function instead):
data sintid2;
set sintid1;
month=month(kalvdat);
if month>=10 then
season=3;
else if month>=5 then
season=2;
else if not missing(month) then
season=1;
run;
What you are missing is that the format does not change the actual value of a variable, only how it is displayed.
What you want to do is probably something like this (using the month function instead):
data sintid2;
set sintid1;
month=month(kalvdat);
if month>=10 then
season=3;
else if month>=5 then
season=2;
else if not missing(month) then
season=1;
run;
Thank you so much! 🙂
Out of interest, why is there only 3 seasons?
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.