I have a sample data in this format as char.
Month
2008-09
2007-10
2008-09
How to get the above as date '01sep2008' (SAS DESIGN STUDIO)
There are a few ways to do this. Dirk has a good method concatenating a day onto the string and using the INPUT() function.
You could also use the yymmn6. informat with the INPUT(). That requires you to strip the '-' from the string.
data test;
x = '2008-05';
format y date9.;
y = input(compress(x,"-"),yymmn6.);
put y;
run;
You could use the MDY() to specify the Month, Day and Year. Here you use the SCAN() function to pick out the month and year from the string using '-' as a delimiter.
data test;
x = '2008-05';
format y date9.;
y = mdy(scan(x,2,'-'),1,scan(x,1,'-'));
put y;
run;
Use an input function
An input function is a function that interpretes a character variable using an informat. _(This is not exactly the same as an input statement, which interpretes text in an infile according to an informat.)
There might be an informat that imidiately interpretes strings like 2008-09 correctly, but I don't know one, so I just add a _01 to it and apply the informat yymmdd10.
Example
Depending on your situation (the data is inform a file or a dataset / you prefer data steps or sql / ...) the full solution would look like this:
data Before;
input the_date $7.;
datalines;
2008-09
2007-10
2008-09
;
data After;
set Before (rename=(the_date=old_date));
format the_date date9.;
the_date = input(old_date||'-01', yymmdd10.);
* while testing the code, uncomment the following line :
drop old_date;
proc print;
run;
Related
I am trying to convert a variable with this format 02FEB22:00:00:00
to yyyymmddhhmmss. I have tried it several different ways and can't seem to get it right. Can anyone help me?
Datetime variables just contain the number of seconds since 1960. There is no need to convert the variable if you want to display it with a different format.
You could create a custom format that will display the current numeric variable as that 14 digit string using a picture format:
proc format ;
picture ymdhms (default=14)
low-high = '%Y%0m%0d%0H%0M%0S' (datatype=datetime)
;
run;
And then just use that new format to display the current variable.
proc print data=have;
format dtvar ymdhms. ;
run;
If you wanted to convert it to a character variable that contains that 14 digit string you could just remove the T from the string created by the B8601DT15. format.
data want;
set have;
length charvar $14;
charvar=compress(put(dtvar,b8601dt15.),'T');
run;
I have a column which is character format and has entries as hh:mm:ss. How can I convert character format to time format using proc sql in sas?
You should be able to use the input function with a format (time8.) to convert the value. The original column will not change its type from char, so you can create another column to hold the numeric value.
If you need something else then please edit your question with an expanded explanation and an example.
/* set up data */
data have;
input char_time : $8.;
datalines;
00:00:00
01:02:03
23:59:59
;
/* create a column in time8. format */
proc sql noprint;
create table
want as
select
char_time
,input(char_time,time8.) as num_time format = time8.
from
have
;
quit;
I am writing a program in Proc SQL. the program takes various input files, where the filenames change from month to month (i.e. myfile_YYYY_MM or mytable_YYYYMM, or mydata_YY_M).
Most of the program is now dynamic, and the user is asked to input reporting date via a prompt when executing the program.
The "Prompt Date" (&Rep_Date.) in the code has format DD MON YYYY (31may2018), and I need to set up one macro variable transforming this value to format YYMMn6 (i.e. 201805).
The syntax looks like this:
%let Period = input("&Rep_Date."d,YYMMN.);
This worked when trying to create a d9 variable, and for creating a month/year variable, like this:
%let date = "&Rep_Date."d; /*Last day execution month*/
%let year = %sysfunc(year("&Rep_Date."d));
%let month = %sysfunc(month("&rep_Date."d));
for some reason, the same does not work when trying to format the date to YYMMn6.
I also tried creating the variable "Period" in a temporary SAS table like this, but again no luck:
Data dates;
Period = input((&Rep_date.,6.), yymmn6.);
format Period yymmn6.;
Run;
Any ideas on where I am going wrong?
I believe the problem is because of the fact that you are using just the input function in your %let statement.. This won't resolve properly. The right thing would be to use the %sysfunc macro function with the input function. The issue is that sysfunc doesn't work with the input function. Hence, the solution is to use %sysfunc(putn()). Here is an example.
Edit:
Not sure what you're trying to achieve but the %window function (assuming you're using Windows) allows you to take input through a prompt and then create a macro variable from the input. Here is an example:
%global Period;
%window info
#5 #5 'Please enter date:'
#5 #40 _Date 9 attr=underline;
%display info;
%put &_Date.;
%macro da(Rep_Date=&_Date);
%let Period = %sysfunc(putn("&Rep_Date."d,YYMMN.));
%put &Period.;
%mend;
%da();
This should work.
What is going wrong ? You are mixing contexts and misconstruing a representation as a literal (literally - pun literally intended).
In short
Have your macro code utilize the date representation value as the basis for a literal value that is used in a putN() invoked by %sysfunc.
%let Rep_Date = 18-JUN-2018; * some date representation;
%let Period = %sysfunc(PUTN("&Rep_Date."D,YYMMN.));
%put NOTE: Period=%superq(Period);
In Long
From the Glossary in "SAS Help and Documentation" (F1)
SAS date valuean integer that represents a date in SAS software. The integer represents the number of days between January 1, 1960, and another specified date. For example, the SAS date value 366 represents the calendar date January 1, 1961.
SAS date constanta string in the form 'ddMMMyy'd or 'ddMMMyyyy'd that represents a date in a SAS statement. The string is enclosed in quotation marks and is followed by the character d (for example, '6JUL01'd, '06JUL01'd, '6 JUL2001'd, or '06JUL2001'd).
date and time formatinstructions that tell SAS how to write numeric values as dates, times, and datetimes.
date and time informatthe instructions that tell SAS how to read numeric values that are represented as dates, times, and datetimes.
Textual values of the form DD MON YYYY is one form of a representation of a date. The form with four year digits can be input using the instructions inherent in the informat DATE11.. The SAS date literals (SAS date constant) are of the form DDMONYYYYD with or without a variety of spaces, dashes, slashes, etc.
Another code variation similar to the 'In short' would input the date text using a specific format (date11.) instead of relying on the system doing a date literal interpretation.
%let Rep_Date_text_representation = 18/JUN/2018;
%let Rep_Date_value = %sysfunc(inputN(&Rep_Date_text_representation,date11.));
%let Period = %sysfunc(putn(&Rep_Date_Value,yymmn.));
%put NOTE: &=Rep_Date_text_representation ;
%put NOTE: &=Rep_Date_value;
%put NOTE: &=Period;
what is the best to format full month name to integer in SAS?
'January' --> 1
'February' --> 2
You could do this without a format:
data test;
monthtext="January";
month=month(input("01"||substr(monthtext,1,3)||"2000",date9.));
run;
You can make your own format:
options fmtsearch=(work);
proc format;
invalue MonNum
JANUARY = 1
FEBRUARY = 2
;
run;
data Month;
length month $10;
input Month $;
month=upcase(month);
monthnum=input(month,monnum.);
datalines;
January
February
;
Run;
Proc report data=work.month nowd;
column month monthnum;
run;
An approach using input function to convert character type to numerical type. But I will agree that creating custom format is better then.
data test;
input monthchar $15.;
datalines;
December
January
March
;
run;
data test;
set test;
monthnum=month(input(cats(1,substr(monthchar,1,3),2000),date9.));
run;
It depends on how you are going to use this in your code. If you need to repeat this mapping in multiple sections of your code then I would suggest creating a custom format using PROC FORMAT which can be used in data steps or in other procedures. If you are just doing this mapping in one data step, then you could use SELECT/WHEN or IF/ELSE IF logic to do the same. There are many other ways to accomplish this in SAS, but I think these two methods are the most straight forward.
Hye Guys,
I'm busy working on a time series and am trying to find the commands that allow me to insert a quarter count variable. To keep things simple, the third quarter of 1995 (date my observations start) should be quarter -2, the fourth quarter of 1995 should be -1 etc etc uptill 2006 (should be somewhere around 45 by then). My dates are in date9 format, such as 20JUN04 etc..
Anyone who can help me with the commands I need t o let this work in SAS?
Thanks
SAS has pretty good built in date and datetime functions. Try this:
/* Some sample data */
data dates;
format dateval date9.;
informat dateval date9.;
input dateval;
datalines;
'01JUL95'
'01OCT95'
'01JAN96'
'20JUN04'
;
run;
/* Sample of the intck function */
data _null_;
set dates;
quarter=intck('qtr','01JAN96'd,dateval);
put _all_;
run;