## character to numeric conversion

Super Contributor
Posts: 284

# character to numeric conversion

Hi,

I want to convert the following list

January

February

March

April

May

June

to the following list -

1

2

3

4

5

6

Thanks,

saspert

Super User
Posts: 9,840

## Re: character to numeric conversion

I would say your options are:

create own format

use a select() when setup (or if's)

Posts: 3,867

## Re: character to numeric conversion

You question if too vague.  While you make it more clear in your follow-up post consider this data step and how it might be used.

data indx(index=(month));
input month :\$16.;
indx = _n_;

cards;
January
February
March
April
May
June
;;;;
run;
proc print;

run;
Super Contributor
Posts: 284

## Re: character to numeric conversion

hi data _null_ - sorry for the vague question. I have a categorical field month in SAS Visual analytics dataset. I was trying to convert to a numeric field so that I could use mdy function. I see your code but how do I use it in SAS VA 7.1?

Thanks,

saspert

Occasional Contributor
Posts: 19

## Re: character to numeric conversion

Hi saspert,

Try creating a calculated item.  In the operators section look for date.  There should be one that says month.  That will return the number of the month.  Don't forget, if your data is coming in with a time, you will need to use the DatePart operator as well.

Mike

Super Contributor
Posts: 284

## Re: character to numeric conversion

Thanks MIke, wouldnt this return an entire date (or alternatively a sas number that stands for a date)? I was looking to convert a month series Jan - Jul to a number series 1-6.

New Contributor
Posts: 3

## Re: character to numeric conversion

A trick I've used...

data want;

input month_txt :\$16.;

month_num = month(input(cats('01', substr(month_txt, 1, 3), '2000'), date9.));

cards;

January

February

March

April

May

June

;;;;

run;

proc print;

run;

Regular Contributor
Posts: 208

## Re: character to numeric conversion

Hi,

You can't show character to numeric format...............

Alternative:

In SAS VA

IF ( 'Month'n In ('Jan') )

RETURN 1

ELSE (

IF ( 'Month'n In ('Feb') )

RETURN 2

ELSE (

IF ( 'Month'n In ('Mar') )

RETURN 3

ELSE (

IF ( 'Month'n In ('Apr') )

RETURN 4

ELSE (

IF ( 'Month'n In ('May') )

RETURN 5

ELSE (

IF ( 'Month'n In ('Jun') )

RETURN 6

ELSE (

IF ( 'Month'n In ('Jul') )

RETURN 7

ELSE (

IF ( 'Month'n In ('Aug') )

RETURN 8

ELSE (

IF ( 'Month'n In ('Sep') )

RETURN 9

ELSE (

IF ( 'Month'n In ('Oct') )

RETURN 10

ELSE (

IF ( 'Month'n In ('Nov') )

RETURN 11

ELSE (

IF ( 'Month'n In ('Dec') )

RETURN 12

ELSE . ) ) ) ) ) ) ) ) ) ) )

You can achieve your requirement by doing above.

Thanks & Regards,

Teja Surapaneni

Super User
Posts: 9,840

## Re: character to numeric conversion

This is not good advice.  Firstly the question has been answered quite simply by either data _null_'s code, or SAS_Mike's response for VA specific.  Secondly, if you need to multiple if statements like that then use select-when syntax:

select('month'n);

when('January') then 1;

...

else...;

end;

Thirdly, even if for some reason you need to use if statements, then the nesting is not necessary, you can use else if, the syntax of which is:

if <condition> then <result>;

else if <condition> then <result>

...

E.g.

if month="January" then 1;

else if month="Febuary" then 2;

...

Finally it would be a good idea to look up good coding practice, e.g: lower casing code - other than SQL it just makes it hard to read.  Consistent indetation.  Align brackets so the set of statements can be clearly identified.

Regular Contributor
Posts: 208

## Re: character to numeric conversion

Hello RW9,

I agree with you. but he wants to calculate in SAS VA.  data_null_'s answer is about back end (SAS EG) data calculation and Mike's Answer is suitable when the columns are in date format.

In front end (SAS VA) we are not able to  change Character to Numeric (we are able to change Date format to numeric format), in this case we have only one alternative i.e.., if else statement  to achieve his requirement.

Thanks & Regards,

Teja Surapaneni

Super Contributor
Posts: 284

## Re: character to numeric conversion

I would agree with Venkata. The coding is fine when you are working in SAS EG. But in SAS VA 7.1, there is no code editor that lets me build a format/data step/select statement.

Super Contributor
Posts: 284

## Re: character to numeric conversion

I initially tried the nested if function. I am surprised you got it to work. I guess I had to have more patience in order for the nesting conditions to work. Only the first if then condition would work for me. I experimented quite a bit for the conversion of months from Jan - Jul to number series 1 - 6.

Occasional Contributor
Posts: 11

## Re: character to numeric conversion

Formats are an option:

proc format;

value \$ monnum 'January' = '1' 'February' = '2' ...

run;

*and in the datastep;

month_num = put(month, \$monnum.) + 0;

(some programmers don't like those automatic conversions :-))

Hth,

ERic

Contributor
Posts: 26

## Re: character to numeric conversion

Its not pretty but the following will work in VA. I would prefer to do these type of calculations on the backend, much simpler.

Month(Parse(Concatenate(Concatenate('01', Substring('MyMonth'n, 1, 3)), '2099'), 'DATE7.'))

Super Contributor
Posts: 284