07-22-2017 12:47 PM
Sas time format beats me. I dont know why it's so complicated. variable STAPERS is YYMMDDN8. format, yymm became best12. How can i convert it to sas time format without changing its style. thanks& regards.
mm=month(STATPERS); yy=year(STATPERS); /*format STATPERS yymmn6.;*/ if mm in (1,2,3) then yymm=yy*100+03; if mm in (4,5,6) then yymm=yy*100+06; if mm in (7,8,9) then yymm=yy*100+09; if mm in (10,11,12) then yymm=yy*100+12;
07-22-2017 12:56 PM
Use a FORMAT statement to assign formats to variables. There is nothing complicated about it. If you run proc contents and don't like a format that you see, change it in the next proc or data step. This next example, as silly as it is, illustrates.
proc print data=sashelp.class;
format age mmddyy8. height 12.6 weight hex16.;
It only changes formats for the print step. Use a format statement in a DATA step to change the format in the data set.
07-22-2017 01:24 PM
Formats do not change numbers. They change how numbers are displayed. Try this. The numbers remain 1 to 10 no matter how they are formatted.
data x; do x = 1 to 10; y = x; z = x; output; end; format x mmddyy8. y date7. z 12.4; run; proc print; run; data y; set x; format x y z; /* clear formats */ run; proc print; run;
07-22-2017 02:07 PM
Sorry, but I don't know what a sas time character is. Date and time variables are saved as numeric variables. They can be displayed with a myriad of formats that do not change the underlying numeric values. Now it is possible to create character variables that have values that we would interpret as times or dates, but I don't think that is what you are asking about. Formats provide rules for mapping the values of numeric (in this case) but also character variables to formatted values, which are character strings that can be displayed in SAS output. You can specify a format for one step, and change it for the next step. I was working with a date/time variable the other day. Sometimes I displayed it using a date time format. Other times, that took too much space and it wasn't important for my purpose (checking my code development) so I removed the format and had it print like an ordinary number. Summary: variables have values, formats display the values in varying ways, and formats do not change the underlying values.
07-22-2017 02:38 PM
I am a new user of sas, and I learned from a book which had said 'the sas variable includes character variable and numeric variable", so I make the mistake. Thank you for letting me know the underlying rules of sas.
07-22-2017 02:47 PM
Right. Variables can be character or numeric. Date/time variables are numeric. Formats display underlying values as character strings using a particular set of rules. There are probably hundreds of formats. Many apply to dates and times. Plus, you can write your own formats. For example you could write a format that displays 1 as 'Disagree', 2 as 'Not Sure', and 3 as 'Agree' (or any one of an infinite number of other possibilities).
07-22-2017 03:17 PM
It's amazing. May I ask you another question? I got a code as following:
data have_base; input company$ _date:$6. profit; date = input(_date !! '01',yymmdd8.); format date yymmn6.; drop _date; datalines; a 199701 5 a 199606 9 b 201404 6 f 200004 78 ; run;
My question is why the coder didn't use a simplier code? I ran the both and found both result are same.
data have_base; input company$ date yymmn6. profit; format date yymmn6.; datalines; a 199701 5 a 199606 9 b 201404 6 f 200004 78 ; run;
07-22-2017 04:02 PM
I don't know. I wish I could say that I always wrote the simplest possble code but even after 38 years of experience with SAS, I don't always think of the best way to do things. Probably the person who wrote it did not know the best way to handle that particular date issue. It is good that you tried it two ways and learned something new about dates. I don't think I have ever encountered that particular form of date input before. Cheers.
07-22-2017 06:15 PM - edited 07-22-2017 06:16 PM
You heading mentions TIME formats, but your example clearly is talking about DATE values, not TIME values. If your variable is actually a DATE value then its value will have the number of days since beginning of 1960. If it was a TIME value then it would have the number of seconds since midnight. It it was a DATETIME value then it would have the number of seconds since 1960.
You can attach a different format to a variable to have it print in many ways. Try this little exmple to see.
data _null_; x=date(); length format $12 str $30 ; do format='comma12.','date9.','yymmdd10.','yymm7.','yyq6.','year.','month.','qtr.'; str=putn(x,format); put format @15 str ; end; run;
What is your actual question? What does your input data look like and what data to you want out?
07-22-2017 11:24 PM
Hi Tom, sorry I confused time character with date character. My input data is date, and my question is very simple. I just want the result after caculation is yymmn6. format. The process likes this
I want yymm to be yymmn6.
07-23-2017 09:37 AM
You can get dates to appear in YYYYMM format by using the YYMMN6. format.
If you want the values stored as strings then just use the PUT() function. If you want the value stored as a number you an either use INPUT() function on the string generated by the PUT() function or in this case just use the YEAR() and MONTH() functions and you arithmetic expression.
datestr= put(date,yymmn6.); date_like_num = input(put(date,yymmn6.),6.); date_like_num = 100*year(date)+month(date) ;
From the example code you posted it looks like you actually want to convert to the last month of the quarter instead. You can use the INTNX() function to do that for you. So if you want Jan, Feb, and Mar to appear as 03 you could find the last day of the quarter.
newdate = intnx('qtr',date,0,'e');
You could then convert NEWDATE to your YYYYMM string or YYY,YMM number.