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

Dear all,

in my sas data I have a variable date YYMMDDN8.

e.g. 19821031

I want to generate year and month variables

year =1982

month=10

I tried and failed with the following:

data test;                                  

   date=19821031;                           

   sasdate=input(put(date,8.),yymmddn8.);    

                                            

   year=year(sasdate);                      

   month=month(sasdate);                    

run;                               

could you make some suggestions?

thanks,

Lan

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

A SAS date value is nothing else than the number of days since 1/1/1960. This value is stored as a number in a numeric variable. You then apply a SAS Date Format to this variable so that the number prints as a human readable date string. The format is only for printing (looking at the number), it doesn't change the value stored.

Your proc contents shows us that you're dealing with a numeric variable with a date format assigned. So what you see (eg: 19811231) is not the value stored in this variable (eg: 8035 as 8035 days since 1/1/1960).

Have a look at below code. May be that will make it clearer to you.

ods listing;

libname source (work);

data source.ib;

  attrib fpedats length=8 format=yymmddn8.;

  fpedats='31DEC1981'd;

  output;

run;

data test;

  set source.ib;

  month=month(fpedats);

  year=year(fpedats);

  /* using formats to print the internal value of fpedats */

  file print;

  put "Number: " fpedats best32.;

  put "Date9: "  fpedats date9.;

  put "yymmddn8: " fpedats yymmddn8.;

  put "Month: " fpedats month.;

  put "Year: " fpedats year.;

run;

proc print data=test;

run;

Output

Number: 8035

Date9: 31DEC1981

yymmddn8: 19811231

Month: 12

Year: 1981


Obs     fpedats    month    year

  1     19811231      12     1981

View solution in original post

15 REPLIES 15
ballardw
Super User

Is the variable a sas variable with that format?

If so then you only need

Year = year(variable);

Month= month(variable);

Or is it a generic number with a value like 19821031?

then try yymmdd8.

Tom
Super User Tom
Super User

How to convert depends if your variable is

1) Character. '19821031'

2) Numeric: 19,821,031

3) Numeric with a date format attached:  such as DATE9. (31OCT1982), YYMMDD10. (1982-10-31) or MMDDYY10 (31-10-1982) , etc .

4) Numeric with a datetime format attached: such as DTDATE9. (31OCT1982).

The YEAR() and MONTH() function assume number 3.

Your assignment statement to set an example value assumes number 2. 

Your example INPUT() function assumes number 1, but it should also work for number 2, it will just generate a note that it converted the number 19,821,031 to the string '19821031' so that the INPUT() function could operate on it.

If you have number 4 then you need to use the DATEPART() function to convert from DATETIME value to a DATE value before applying the YEAR() or MONTH() function.

Also note that if you actually have a number you can use INT() and MOD() functions to convert.

year = int(date/10000) ;

month = int(mod(date,10000)/100) ;

slchen
Lapis Lazuli | Level 10

data test;                                 

   date=19821031;                          

   sasdate=input(put(date,8.),yymmdd8.)  ;

   year=year(sasdate);                     

   month=month(sasdate);

   format sasdate yymmddn8.;

run;         

Patrick
Opal | Level 21

Except from an amendment to the informat your code worked fine for me "as is"

data test;

  date=19821031;

  sasdate=input(put(date,8.),yymmdd8.);

  year=year(sasdate);

  month=month(sasdate);

run;

OS2Rules
Obsidian | Level 7

Hi:

Change the format in the input statement to "yymmdd8." and try again.

Worked for me.

LanMin
Fluorite | Level 6

Thanks to everyone !

my date variable is numeric, with lengthn8 , yymmddn8 format, I did what Patrick suggested, it works in his example, but for my data set , it did not work .note he was using yymmdd8. , my format is yymmddn8. I am not sure if that is a problem.

art297
Opal | Level 21

If you already have a variable that has a SAS date, and (e.g.) let's assume it is called DATE, then all you need is:

  year=year(date);

  month=month(date);

The format is totally irrelevant to what you are trying to do.

LanMin
Fluorite | Level 6

Hi Arthur,

My date variable is not sas date,

I rerun my code based on Patrick and OS2rules advice. I think the error was caused by missing values for some observations for date variable.

I try to drop missing dates (note: it is numeric). this did not work

drop if FDATES=.;

below is without dropping missing

106  data test;

107  set source.ib;

108     sasdate=input(put(FDATES,8.),yymmdd8.);

109

110     year=year(sasdate);

111     month=month(sasdate);

112  run;

NOTE: Invalid argument to function INPUT at line 108 column 12.

FPEDATS=19811231 CUSIP=80556710 ANALYS=008474 sasdate=. year=. month=. _ERROR_=1 _N_=1

NOTE: Invalid argument to function INPUT at line 108 column 12.

FPEDATS=19811231 CUSIP=80556710 ANALYS=008474 sasdate=. year=. month=. _ERROR_=1 _N_=2

NOTE: Invalid argument to function INPUT at line 108 column 12.

FPEDATS=19820331 CUSIP=  ANALYS=030130 sasdate=. year=. month=. _ERROR_=1 _N_=3

LanMin
Fluorite | Level 6

Hi everyone,

please   allow me to correct my last post, I thought it was missing date value that is causing the error, but it is not.

my date variable name is FPEDATS

PROC CONTENTS  shows :

1FPEDATSNum8YYMMDDN8.Forecast Period End Date, SAS Format

25   data test;

26

27   set source.ib;

28      sasdate=input(put(FPEDATS,8.),yymmdd8.);

29      run;

NOTE: Invalid argument to function INPUT at line 28 column 12.

FPEDATS=19811231 CUSIP=80556710 ANALYS=008474 sasdate=. _ERROR_=1 _N_=1

NOTE: Invalid argument to function INPUT at line 28 column 12.

FPEDATS=19811231 CUSIP=80556710 ANALYS=008474 sasdate=. _ERROR_=1 _N_=2

NOTE: Invalid argument to function INPUT at line 28 column 12.

FPEDATS=19820331 CUSIP=  ANALYS=030130 sasdate=. _ERROR_=1 _N_=3

NOTE: Invalid argument to function INPUT at line 28 column 12.

FPEDATS=19820331 CUSIP=00253510 ANALYS=001877 sasdate=. _ERROR_=1 _N_=4

NOTE: Invalid argument to function INPUT at line 28 column 12.

FPEDATS=19820331 CUSIP=  ANALYS=031231 sasdate=. _ERROR_=1 _N_=5

NOTE: Invalid argument to function INPUT at line 28 column 12.

FPEDATS=19820331 CUSIP=58719010 ANALYS=031231 sasdate=. _ERROR_=1 _N_=6

NOTE: Invalid argument to function INPUT at line 28 column 12.

FPEDATS=19820630 CUSIP=02635310 ANALYS=008856 sasdate=. _ERROR_=1 _N_=7

NOTE: Invalid argument to function INPUT at line 28 column 12.

FPEDATS=19820630 CUSIP=  ANALYS=008118 sasdate=. _ERROR_=1 _N_=8

NOTE: Invalid argument to function INPUT at line 28 column 12.

FPEDATS=19820930 CUSIP=  ANALYS=001652 sasdate=. _ERROR_=1 _N_=9

NOTE: Invalid argument to function INPUT at line 28 column 12.

FPEDATS=19820930 CUSIP=  ANALYS=000849 sasdate=. _ERROR_=1 _N_=10

NOTE: Invalid argument to function INPUT at line 28 column 12.

FPEDATS=19821031 CUSIP=05276010 ANALYS=008668 sasdate=. _ERROR_=1 _N_=11

NOTE: Invalid argument to function INPUT at line 28 column 12.

FPEDATS=19821031 CUSIP=45282420 ANALYS=009105 sasdate=. _ERROR_=1 _N_=12

NOTE: Invalid argument to function INPUT at line 28 column 12.

FPEDATS=19821130 CUSIP=00205010 ANALYS=008856 sasdate=. _ERROR_=1 _N_=13

NOTE: Invalid argument to function INPUT at line 28 column 12.

FPEDATS=19821130 CUSIP=00205010 ANALYS=031019 sasdate=. _ERROR_=1 _N_=14

NOTE: Invalid argument to function INPUT at line 28 column 12.

FPEDATS=19821130 CUSIP=00205010 ANALYS=017636 sasdate=. _ERROR_=1 _N_=15

NOTE: Invalid argument to function INPUT at line 28 column 12.

FPEDATS=19821130 CUSIP=00205010 ANALYS=030009 sasdate=. _ERROR_=1 _N_=16

NOTE: Invalid argument to function INPUT at line 28 column 12.

FPEDATS=19821130 CUSIP=77952810 ANALYS=000031 sasdate=. _ERROR_=1 _N_=17

NOTE: Invalid argument to function INPUT at line 28 column 12.

FPEDATS=19821130 CUSIP=80936710 ANALYS=004278 sasdate=. _ERROR_=1 _N_=18

NOTE: Invalid argument to function INPUT at line 28 column 12.

FPEDATS=19821130 CUSIP=80936710 ANALYS=008048 sasdate=. _ERROR_=1 _N_=19

NOTE: Invalid argument to function INPUT at line 28 column 12.

WARNING: Limit set by ERRORS= option reached.  Further errors of this type will not be printed.

FPEDATS=19821130 CUSIP=80936710 ANALYS=000712 sasdate=. _ERROR_=1 _N_=20

NOTE: Mathematical operations could not be performed at the following places. The results of the

      operations have been set to missing values.

      Each place is given by: (Number of times) at (Line):(Column).

      3873824 at 28:12

NOTE: There were 3966889 observations read from the data set SOURCE.IB.

NOTE: The data set WORK.TEST has 3966889 observations and 4 variables.

NOTE: DATA statement used (Total process time):

      real time           1.53 seconds

      cpu time            1.52 seconds

What did I do wrong?

thank you,

Lan

Reeza
Super User

You already have a SAS date.

data test;                                                           

  set source.ib                                          

   year=year(FPEDATS);                      

   month=month(FPEDATS);                    

run;    

Patrick
Opal | Level 21

A SAS date value is nothing else than the number of days since 1/1/1960. This value is stored as a number in a numeric variable. You then apply a SAS Date Format to this variable so that the number prints as a human readable date string. The format is only for printing (looking at the number), it doesn't change the value stored.

Your proc contents shows us that you're dealing with a numeric variable with a date format assigned. So what you see (eg: 19811231) is not the value stored in this variable (eg: 8035 as 8035 days since 1/1/1960).

Have a look at below code. May be that will make it clearer to you.

ods listing;

libname source (work);

data source.ib;

  attrib fpedats length=8 format=yymmddn8.;

  fpedats='31DEC1981'd;

  output;

run;

data test;

  set source.ib;

  month=month(fpedats);

  year=year(fpedats);

  /* using formats to print the internal value of fpedats */

  file print;

  put "Number: " fpedats best32.;

  put "Date9: "  fpedats date9.;

  put "yymmddn8: " fpedats yymmddn8.;

  put "Month: " fpedats month.;

  put "Year: " fpedats year.;

run;

proc print data=test;

run;

Output

Number: 8035

Date9: 31DEC1981

yymmddn8: 19811231

Month: 12

Year: 1981


Obs     fpedats    month    year

  1     19811231      12     1981

Tom
Super User Tom
Super User

Your variable already has DATE values.  You just have the YYMMDDN8. format attached to it instead of the SAS standard DATE9. format. So when you print them or use PUT to display the value it looks like a human readable format.


A date variable that displays as '19811231' when using the YYMMDDn8. format has the numeric value of 8400. 

The INPUT format YYMMDD8. cannot convert '8400' (or the other values in your data) to a valid date.


Since you already have a date you can extract the year and month using the YEAR() and MONTH() functions.

LanMin
Fluorite | Level 6

Many Thanks to Tom, Patrick, Reeza, and everyone else !!!!  I got it now.

Best,

Lan

CharlotteCain
Quartz | Level 8

Hi, It would be courteous if you could mark the question as answered and gift the appropriate points to the respondents for correct and useful answers. I do ask questions and I always make sure to recognise the help. It's only fair!

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 15 replies
  • 65377 views
  • 11 likes
  • 9 in conversation