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
.......
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
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;
thanks for answer...
is there any format through which i can convert it like
x= input('jan',monname3.);
plz guide me..
make your own!
see PROC FORMAT
and its INVALUE statement
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
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;
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
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.