The SAS Output Delivery System and reporting techniques

convert month in num

Accepted Solution Solved
Reply
Super Contributor
Posts: 258
Accepted Solution

convert month in num

data test;

input month $ sale;

datalines;

jan 2000

feb 1400

mar 2300

apr 3400

may 2700

jun 3200

july 800

aug 2900

sep 1700

oct 2100

nov 2600

dec 3200

;

run;

i m looking to convert month in num as 1 2 3 ,

for that i m tring it.

data test1;

set test;

mon_num=input(month,monname3.);

run;

proc print; run;

but its showing error. i need to short data by month. can we do it in short or sud i use if condition on each month????

desired output is

month sale

1 2000

2 1400

3 2300

4 3400

.......


Accepted Solutions
Solution
‎01-26-2013 02:55 PM
SAS Super FREQ
Posts: 8,645

Re: convert month in num

Hi:

  To work with SAS date variables, remember that a SAS date variable is a number that represents the number of days since January 1, 1960. When you use the INPUT function, you are converting a character string or character variable into a SAS numeric variable. When you do the conversion, you must use an INFORMAT to convert your character variable to a number. MONNAME is a FORMAT -- it is used to display a SAS date variable (such as in PROC PRINT, PROC REPORT or PROC TABULATE report) as a 3 character month name. So it would not be appropriate to use MONNAME with an INPUT statement. In fact, you should have gotten this error message in your SAS log:

ERROR 48-59: The informat MONNAME was not found or could not be loaded.

  So, the correct thing to do was to turn your character string into a SAS date variable, so you could use the MONTH function, as Linlin demonstrated.

  When you ask whether there is any FORMAT you can use, with an INPUT function, the answer is NO. You have to use an INFORMAT with the INPUT function. But the larger issue is that 'Jan" and "Feb" are character strings. They are not date values, in SAS terms. Yes, they are the names of months, but the name of the month is not the same thing as a SAS date value. That's why Linlin's solution took the month string and used it to make a real SAS date value (number of days since Jan 1, 1960). Then the month function returned the month number, which is  what you said you wanted.

  Perhaps this Tech Support note will give you a direction. (24651 - Generate the month name from a numeric value) The note doesn't show exactly what you want to do, but the Sample 2 program does show how to write a format to turn a number into a month name. The reverse is also possible.

cynthia

View solution in original post


All Replies
Super Contributor
Posts: 1,636

Re: convert month in num

data test;

input month $ sale;

datalines;

jan 2000

feb 1400

mar 2300

apr 3400

may 2700

jun 3200

jul 800

aug 2900

sep 1700

oct 2100

nov 2600

dec 3200

;

data want;

set test;

num_month=month(input(cats('01',month,'2013'),date9.));

run;

proc print;run;

Super Contributor
Posts: 258

Re: convert month in num

thanks for answer...

is there any format through which i can convert it like

x= input('jan',monname3.);

plz guide me..

Valued Guide
Posts: 2,152

Re: convert month in num

make your own!

see PROC FORMAT

and its INVALUE statement

Solution
‎01-26-2013 02:55 PM
SAS Super FREQ
Posts: 8,645

Re: convert month in num

Hi:

  To work with SAS date variables, remember that a SAS date variable is a number that represents the number of days since January 1, 1960. When you use the INPUT function, you are converting a character string or character variable into a SAS numeric variable. When you do the conversion, you must use an INFORMAT to convert your character variable to a number. MONNAME is a FORMAT -- it is used to display a SAS date variable (such as in PROC PRINT, PROC REPORT or PROC TABULATE report) as a 3 character month name. So it would not be appropriate to use MONNAME with an INPUT statement. In fact, you should have gotten this error message in your SAS log:

ERROR 48-59: The informat MONNAME was not found or could not be loaded.

  So, the correct thing to do was to turn your character string into a SAS date variable, so you could use the MONTH function, as Linlin demonstrated.

  When you ask whether there is any FORMAT you can use, with an INPUT function, the answer is NO. You have to use an INFORMAT with the INPUT function. But the larger issue is that 'Jan" and "Feb" are character strings. They are not date values, in SAS terms. Yes, they are the names of months, but the name of the month is not the same thing as a SAS date value. That's why Linlin's solution took the month string and used it to make a real SAS date value (number of days since Jan 1, 1960). Then the month function returned the month number, which is  what you said you wanted.

  Perhaps this Tech Support note will give you a direction. (24651 - Generate the month name from a numeric value) The note doesn't show exactly what you want to do, but the Sample 2 program does show how to write a format to turn a number into a month name. The reverse is also possible.

cynthia

Valued Guide
Posts: 2,152

Re: convert month in num

and to follow what I said earlier and Cynthia has pointed you at, the SAS System provides the functionality you are asking about - you just have to go use it

data ;

input mth_str M3N3. @1 buffr :$20.  ;

put mth_str= buffr= ;

list;cards;

Jan

january

June

;

will demo the results you want only if you first create that informat .....

perhaps something like

proc format ;

invalue m3n (upcase) JAN = 1 FEB=2 MAR=3 APR=4 MAY=5

JUN=6  OTHER=_ERROR_ ;

RUN;

Contributor
Posts: 30

Re: convert month in num

Hi Cynthia,

 

I am trying to convert a month varibale which is in String/Character format eg(Feb, Mar etc) to Date format of same eg(Feb, Mar)

 

I am also trying to convert a year Variable (string) to Date format. 

Note: Year and Month are two different variables.

 

I kindly request you to help me with this with a sample expample so that i can follow the same code and try it with my case

 

Thanks & regards,

Karthik

SAS Super FREQ
Posts: 8,645

Re: convert month in num

Hi: I am not sure what you are asking and how it is relevant to the previous posting. That older posting was from 2013. If you are asking how to convert the string '2016' to the numeric value 2016, that would be done with an INPUT function. And, if you want to convert a string of 'Jan' to the number 1 and the string 'Feb' to the number 2, then that would also use an INPUT function.

I would suggest that you make a new post instead of re-activating a previously solved post and show a sample of your data and the code that you've tried, so that people know what the new question is.

cynthia
Contributor
Posts: 30

Re: convert month in num

Ohhh I am sorry I did not know that I have to post a new question. Anyways thank you for replying.
SAS Super FREQ
Posts: 8,645

Re: convert month in num

Hi:
Generally, when a previous posting has already been solved and/or is old (more than a few months), it is better to start a new post instead of "waking up" an older post. No worries, if someone doesn't scroll all the way to the bottom, they might miss your current post.

It's just better all around if you have a related question to start a new post and then, you can put a link to the older post in your new posting and say something like, "I saw this previous post here: xxxxx, but it didn't seem to address my question."

cynthia
Post a Question
Discussion Stats
  • 9 replies
  • 8575 views
  • 3 likes
  • 5 in conversation