BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Lisan
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
s_lassen
Meteorite | Level 14

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;

View solution in original post

4 REPLIES 4
s_lassen
Meteorite | Level 14

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;
Lisan
Calcite | Level 5

Thank you so much! 🙂

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Out of interest, why is there only 3 seasons?

Lisan
Calcite | Level 5
1= indoor season, winter feed
2=outdoor season, pasture
3=indoor season, fall feed

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 846 views
  • 0 likes
  • 3 in conversation