Hello everyone,
I am new to the site because I sadly do not know where else to turn to help, when no one seems to not be able to help. I uploaded a document showing the question I am working on and trying to accomplish.
Question g. is what I am stuck on I merged my 2 datasets together I just don't know how to put my months in chronological order. They have to stay in that month name format too, I can't do a 04-march-20 time of format. I was looking around and I have heard about renaming the variables into numbers then being able to sort by that and still keep the name of the month??? I am so lost and I don't know where to start or even what code to start laying out for a foundation. Please help. And thank you guys so much for helping.
Included is my SAS code as well.
Sincerely,
Michael
From the looks of your code, you have MONTH as a character variable, presumably with values like "January", "February", etc, which obviously would not sort chronologically. So you need to create a new variable, say MONTHNUM, with integer values from 1 through 12 (or more generally some value in ascending order corresponding to the order of the month names).
I would suggest a data step that applies the FINDW function on MONTH producing MONTHNUM, as in
MONTHNUM=findw('January February … December',trim(month),' ','?');
Then you could sort by MONTHNUM/DAY to get chronological order.
Note I've left the 3rd argument blank, and the 4th argument with a '?'. Look up the FINDW function and you'll see why I left the 3rd blank. As to the 4th, you'll need to replace the '?' with some other modifier to tell FINDW that you want to count words in the 1st argument while searching for the 2nd argument.
From the looks of your code, you have MONTH as a character variable, presumably with values like "January", "February", etc, which obviously would not sort chronologically. So you need to create a new variable, say MONTHNUM, with integer values from 1 through 12 (or more generally some value in ascending order corresponding to the order of the month names).
I would suggest a data step that applies the FINDW function on MONTH producing MONTHNUM, as in
MONTHNUM=findw('January February … December',trim(month),' ','?');
Then you could sort by MONTHNUM/DAY to get chronological order.
Note I've left the 3rd argument blank, and the 4th argument with a '?'. Look up the FINDW function and you'll see why I left the 3rd blank. As to the 4th, you'll need to replace the '?' with some other modifier to tell FINDW that you want to count words in the 1st argument while searching for the 2nd argument.
Thank you so much for the help, I figured it out via the monthnum way. I did it a little bit different as you can see below, but hey it all works the same right lol. Thank you again for helping me and getting me to think about that way. All is greatly appreciated.
data chrondata; set STOZFinal;
select (Month);
when ('May') monthnum=1;
when ('June') monthnum=2;
when ('July') monthnum=3;
when ('August') monthnum=4;
when ('September') monthnum=5;
otherwise;
end;
run;
Welcome to the SAS Communities. It's totally o.k. to ask such questions here - also for assignments as long as you've done your bit and tried first yourself to solve the problem.
Open your source data with a text editor and copy/paste a few lines here using the </> icon. Or if the .dat file isn't too big then just attach the file to your question.
Depending on how your source data looks reading the "month" column differently into SAS might resolve the issue.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.