how to use year and month function

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 15
Accepted Solution

how to use year and month function

here is my codes.it have some miskates,but i can not find them.please do me a favour. thank you very much.

data raw;

   input stckcd 1. @3 date yyyymm7.+1return 2.;

cards;

1 1995-01 1

1 1995-02 2

1 1995-03 3

1 1995-04 4

1 1995-05 5

1 1996-01 11

1 1996-02 22

1 1996-03 33

1 1996-04 44

2 1995-01 6

2 1995-02 7

2 1995-03 8

2 1995-04 9

2 1996-01 66

2 1996-02 77

2 1996-03 88

;

run;

data raw1;

set raw;

year=year(date);

month=month(date);

run;

proc print data=raw1;run;

here is the results

                         Obs    stckcd     date    return    year    month

                           1       1      11995       1      1992      11

                           2       1      21995       2      2020       3

                           3       1      31995       3      2047       8

                           4       1      41995       4      2074      12

                           5       1      51995       5      2102       5

                           6       1      11996      11      1992      11

                           7       1      21996      22      2020       3

                           8       1      31996      33      2047       8

                           9       1      41996      44      2074      12

                          10       2      11995       6      1992      11

                          11       2      21995       7      2020       3

                          12       2      31995       8      2047       8

                          13       2      41995       9      2074      12

                          14       2      11996      66      1992      11

                          15       2      21996      77      2020       3

                          16       2      31996      88      2047       8


Accepted Solutions
Solution
‎03-02-2013 10:16 AM
Super Contributor
Posts: 1,636

Re: how to use year and month function

data raw;

   input stckcd 1. @3 date $7.+1return 2.;

cards;

1 1995-01 1

1 1995-02 2

1 1995-03 3

1 1995-04 4

1 1995-05 5

1 1996-01 11

1 1996-02 22

1 1996-03 33

1 1996-04 44

2 1995-01 6

2 1995-02 7

2 1995-03 8

2 1995-04 9

2 1996-01 66

2 1996-02 77

2 1996-03 88

;

run;

data raw1;

set raw;

year=year(mdy(input(scan(date,2),2.),1,input(scan(date,1),4.)));

month=month(mdy(input(scan(date,2),2.),1,input(scan(date,1),4.)));

run;

proc print data=raw1;run;

             Obs    stckcd     date      return    year    month

                  1       1      1995-01       1      1995      1

                  2       1      1995-02       2      1995      2

                  3       1      1995-03       3      1995      3

                  4       1      1995-04       4      1995      4

                  5       1      1995-05       5      1995      5

                  6       1      1996-01      11      1996      1

                  7       1      1996-02      22      1996      2

                  8       1      1996-03      33      1996      3

                  9       1      1996-04      44      1996      4

                 10       2      1995-01       6      1995      1

                 11       2      1995-02       7      1995      2

                 12       2      1995-03       8      1995      3

                 13       2      1995-04       9      1995      4

                 14       2      1996-01      66      1996      1

                 15       2      1996-02      77      1996      2

                 16       2      1996-03      88      1996      3

View solution in original post


All Replies
Solution
‎03-02-2013 10:16 AM
Super Contributor
Posts: 1,636

Re: how to use year and month function

data raw;

   input stckcd 1. @3 date $7.+1return 2.;

cards;

1 1995-01 1

1 1995-02 2

1 1995-03 3

1 1995-04 4

1 1995-05 5

1 1996-01 11

1 1996-02 22

1 1996-03 33

1 1996-04 44

2 1995-01 6

2 1995-02 7

2 1995-03 8

2 1995-04 9

2 1996-01 66

2 1996-02 77

2 1996-03 88

;

run;

data raw1;

set raw;

year=year(mdy(input(scan(date,2),2.),1,input(scan(date,1),4.)));

month=month(mdy(input(scan(date,2),2.),1,input(scan(date,1),4.)));

run;

proc print data=raw1;run;

             Obs    stckcd     date      return    year    month

                  1       1      1995-01       1      1995      1

                  2       1      1995-02       2      1995      2

                  3       1      1995-03       3      1995      3

                  4       1      1995-04       4      1995      4

                  5       1      1995-05       5      1995      5

                  6       1      1996-01      11      1996      1

                  7       1      1996-02      22      1996      2

                  8       1      1996-03      33      1996      3

                  9       1      1996-04      44      1996      4

                 10       2      1995-01       6      1995      1

                 11       2      1995-02       7      1995      2

                 12       2      1995-03       8      1995      3

                 13       2      1995-04       9      1995      4

                 14       2      1996-01      66      1996      1

                 15       2      1996-02      77      1996      2

                 16       2      1996-03      88      1996      3

Occasional Contributor
Posts: 15

Re: how to use year and month function

thank you very much!!

i think i will have many problems in future, I look forward to your enthusiastic help。

Super User
Super User
Posts: 7,076

Re: how to use year and month function

There is no YYYYMM informat. Try using ANYDTDTE instead.

data raw;

  informat date anydtdte. ;

  format date date9.;

  input stckcd date return ;

  year=year(date);

  month=month(date);

cards;

1 1995-01 1

1 1995-02 2

1 1995-03 3

1 1995-04 4

1 1995-05 5

1 1996-01 11

1 1996-02 22

1 1996-03 33

1 1996-04 44

2 1995-01 6

2 1995-02 7

2 1995-03 8

2 1995-04 9

2 1996-01 66

2 1996-02 77

2 1996-03 88

run;

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 226 views
  • 5 likes
  • 3 in conversation