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

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

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

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.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

3 REPLIES 3
mkeintz
PROC Star

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.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
mthomas2014
Fluorite | Level 6

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;

Patrick
Opal | Level 21

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. 

Patrick_0-1592874378630.png

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 943 views
  • 1 like
  • 3 in conversation