BookmarkSubscribeRSS Feed
saspert
Pyrite | Level 9

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

14 REPLIES 14
RW9
Diamond | Level 26 RW9
Diamond | Level 26

I would say your options are:

create own format

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

data_null__
Jade | Level 19

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;
saspert
Pyrite | Level 9

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

SAS_Mike
Obsidian | Level 7

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

saspert
Pyrite | Level 9

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.

cmepdx
Calcite | Level 5

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;

TejaSurapaneni
Lapis Lazuli | Level 10

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 . ) ) ) ) ) ) ) ) ) ) )

Untitled.png

You can achieve your requirement by doing above.

Thanks & Regards,

Teja Surapaneni

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

TejaSurapaneni
Lapis Lazuli | Level 10

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

saspert
Pyrite | Level 9

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.

saspert
Pyrite | Level 9

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.

EricHoogenboom
Fluorite | Level 6

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

RichardPaterson
Obsidian | Level 7

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.'))

saspert
Pyrite | Level 9

Thanks - I was looking for somehting like this. I actually did a very crude conversion in SAS VA. There is a facility to name a new custom category and then use a choice of labels. So what I did was add 6 labels 1-6 and then assign the months Jan - Jul for that. I have to see if that will create further unexpected issues in which case I will try your formula. After creating this custom category, I created another calculated item which took this custom category as input for mdy function. For the year argument, I used 2015. For the date, I had a field from the sas dataset.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

Tips for filtering data sources in SAS Visual Analytics

See how to use one filter for multiple data sources by mapping your data from SAS’ Alexandria McCall.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 14 replies
  • 5327 views
  • 3 likes
  • 8 in conversation