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

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

.......

1 ACCEPTED SOLUTION

Accepted Solutions
Cynthia_sas
SAS Super FREQ

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

9 REPLIES 9
Linlin
Lapis Lazuli | Level 10

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;

Aman4SAS
Obsidian | Level 7

thanks for answer...

is there any format through which i can convert it like

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

plz guide me..

Peter_C
Rhodochrosite | Level 12

make your own!

see PROC FORMAT

and its INVALUE statement

Cynthia_sas
SAS Super FREQ

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

Peter_C
Rhodochrosite | Level 12

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;

Karthikk
Fluorite | Level 6

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

Cynthia_sas
SAS Super FREQ
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
Karthikk
Fluorite | Level 6
Ohhh I am sorry I did not know that I have to post a new question. Anyways thank you for replying.
Cynthia_sas
SAS Super FREQ
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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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
  • 9 replies
  • 21072 views
  • 5 likes
  • 5 in conversation