I have two numeric variables, year and month. year variable has data such as 2010 and month variable has data such as 1 and 10 (1 through 9 doesn't have zero at the front). I need to combine these two variables and then convert it to YYMMn6. format so that I can merge another dataset based on the date.
For example, the input is:
2012 1
2012 10
The output I want is (in YYMMn6. format):
201201
201210
The codes I tried so far:
year1=close_year;
year2=clse_month;
yearmonth = cats(of year1-year2); *this results in character variable;
DATE2 = INPUT(PUT(yearmonth,8.),YYMMN6.);
FORMAT DATE2 YYMMN6.;
Of course I get an error message. Thanks.
With numeric variables I'd use MDY function rather than putting and whatnot; you're having trouble here because 20101 isn't a valid YYMM value.
dateval = mdy(monthval,1,yearval);
format dateval yymmn6.;
Note that the 'final' date format is wholly unrelated to whatever you use to input the date variable from an informat; there's no difference from SAS's point of view between
dateval = input('01JAN2010',DATE9.);
format dateval YYMMN6.;
and
dateval = input('201001',YYMMN6.);
format dateval YYMMN6.;
The input/informat is converting a value into a numeric number of days since 1/1/1960. The final format is telling SAS how to display that newly created number.
You can use the answer mentioned by Joe which
would give you the flexibility to change to a different format if you want later on,
without any hassle.
would keep the variables in numeric format, so mathematical or
date functions would be easy to apply.
or you can use
mydate=put(mdy(monthval,1,yearval),yymmn6.);
if you want the output in char format.
Both are correct. Choose as per your requiremnt.
Related
how can I convert a date (character) variable = 22249 (=30NOV2020) into 30NOV2020 (numeric)?
I tried many ways like input(), put() and so on but without success. Also setting date9., SAS output is: ERROR the format $date9 was not found or could not be loaded.
Many thanks in advance
Best
To convert a string like '22249' into a number like the date '30NOV2020'd just use the normal numeric informat. Once you have numeric variable with the value 22,249 you can attach any date type format to it to have it be displayed in a way that humans would recognize as a date.
data test;
string='22249';
date = input(string,32.);
format date date9.;
run;
Result
Obs string date
1 22249 30NOV2020
I have a column which has mixed values of month and date (its in character $5 format).
date
7/23
5/23
23MAR
7/19
I want the data to come as uniform date5. format like this
date
23MAR
23MAY
23MAR
19JUL.
Here is the code that I'm using
data DAte_check4again;
set Date_2test;
format check_dt date5.;
check_dt=datepart(date);
run;
SAS stores DATE, TIME and DATETIME values as numbers. The DATEPART() function you are trying to use is for converting DATETIME values to DATE values. But your source variable is character with a length of 5. (FORMATs are just instructions for how to display values).
So your first problem will be to convert the string into a DATE value. You can then take the first 5 characters of the DATE. format and store that into either your original variable or some other variable. Assuming that the month/day values are for the current year and you only have those two styles of strings here is one method to generate a date and also the 5 character string.
data want;
set have ;
if index(date,'/') then date_ck = input(cats(date,'/',year(today())),mmmddyy10.);
else date_ck = input(cats(date,year(today())),date9.);
format date_ck date9.;
new_date = substr(put(date_ck,date9.),1,5);
run;
I need to change what it is currently at into the date9 format.
input Company_Name$ 1-58 Form_Type$ 59-70 CIK Date_filed$ 86-96 File_Name$ 118-141;
length Date_filed $10.;
format Date_filed yymmdd10.;
ndate=put(Date_filed, yymmdd10.);
this is what I have, but it doesn't work. I am not sure how to do this.
SAS stores dates in numeric variables as the number of days since 1960. Once you have a valid date value you can attach any of the many formats that change how the value is displayed. To read directly from the source file into a date value using an INFORMAT that is appropriate for the value. Note that you cannot combine an informat with the column range in the input statement. Instead you can use # to position at the first column of the date string and then use formatted input. Also you did not tell INPUT what columns to read for the variable CIK.
input Company_Name $ 1-58 Form_Type $ 59-70
CIK 71-85 #86 Date_filed yymmdd10.
File_Name $ 118-141
;
format Date_filed date9.;
Note you were reading 11 columns before but date strings in YYYY-MM-DD style only need 10 columns, so check if your date strings are in columns 86-95 or columns 87-96. If you don't read the right columns you might miss the second digit of the day of the month or the first digit of the year.
I have a column that contains date values. So when imported as numeric, it shows 20668, 20669...etc. if I format it as yymmddn8, it shows 20160802 etc. However, what I really want is a numeric variable that shows 20160802. I have tried to create other to get day, month, year and then concatenate them. Unfortunately, the issue is if month and day is 1 digit, it would only show 201682. what would be the quickest way to achieve my goal. I guess a can turn the day and month variable to text and add 0 if day or month is less than 10. But this is not elegant and efficient. Please help.
Thanks
You can just wrap an input around that format:
data test;
date = 20668;
full_date = input(put(date,yymmddn8.),best12.);
run;
The put is converting the date to character in the format as you want it to appear, and the input with the best12. format is converting it back to numeric in that format.
It sounds like you just need to attach a format to your variable.
format date yymmddn8. ;
Try running this program to see a few of the different formats that are available for displaying dates.
data _null_;
do date = 20668, 20669 ;
put (6*date) (=10. =date9. =yymmddn8. =mmddyy10. =ddmmyy10. =yymmdd10.) ;
end;
run;
How do I convert 06JAN2005(as date format) to 200501 (as a number)
Note that there are only two different variable types in SAS - character and numeric. Dates are stored in numeric variables and are generally formatted to appear as user-readable dates. Specifically, dates are stored as the number of days since 01JAN1960.
So if you have a number already formatted as a date, and would like it to appear formatted differently (but still be a number) you simply need to change the format.
As Joe's comment says - I can't see the relationship between the date you provided and the number you desire (maybe you have a typo?). But the below piece of code will demonstrate how you can simply change the format to change the appearance of the number:
data _null_;
format date1 best.
date2 yymmddn6.
date3 date9.
;
date1 = '06JAN2005'd;
date2 = '06JAN2005'd;
date3 = '06JAN2005'd;
put date1= date2= date3=;
run;
Gives:
date1=16442 date2=050106 date3=06JAN2005
If you want an explicit numeric value, you can use the date functions on your date variable which, as pointed out, is a numeric date value SAS understands.
newdatevar=100*year(olddatevar)+month(olddatevar)
Well if you mean you need year||month then something like this should work:
data two;
set one;
order_month = month(date);
order_year = year(date);
order = put(compress(order_month||order_year),6.)-0;
run;
Disclaimer being I'd reccomend just formatting whatever else you have properly, so you don't need to represent the date as a number. If you're looking to add or substract increments of dates, you might want to look at the intx function too
Try this:
data a;
attrib
v_input format=date.
v_output_n format=8.;
v_input = '06JAN2005'd;
v_output_n = put(v_input,yymmn6.);
run;