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
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
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.
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) ;
data test;
date=19821031;
sasdate=input(put(date,8.),yymmdd8.) ;
year=year(sasdate);
month=month(sasdate);
format sasdate yymmddn8.;
run;
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;
Hi:
Change the format in the input statement to "yymmdd8." and try again.
Worked for me.
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.
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.
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
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 :
1 | FPEDATS | Num | 8 | YYMMDDN8. | 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
You already have a SAS date.
data test;
set source.ib
year=year(FPEDATS);
month=month(FPEDATS);
run;
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
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.
Many Thanks to Tom, Patrick, Reeza, and everyone else !!!! I got it now.
Best,
Lan
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!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.