Hi all,
I have three separate variables: day, month, and year.
I would like to construct a variable that retains the first occurrence of a certain value, and then outputs the date of the occurrence in the form of MM-DD-YYYY.
Does anyone know how I would go about this?
Much thanks in advance!!
Look at mdy function (SAS(R) 9.2 Language Reference: Dictionary, Fourth Edition )
Thanks! Bear with me, I am new to sas:
I did see this on another thread:
Well, it seems working for me, please see:
data have;
infile cards;
informat date $80.;
input date ;
cards;
05/21/2010
07/01/2010
03/01/2011
;
data want;
set have;
format _date mmddyy10.;
_date=input(trim(date),MMDDYY10.);
run;
proc print;run;
My suspicion is that you can't just convert char to numeric under the SAME variable name. You have to create another variable to the job, and if you want your old name back, you can always do it using date step options like: drop= rename=.
However, without seeing your code and log, I can be totally off on this one.
Regards,
Haikuo
However my problem is my date, year, and month are each a different variable, it is not together as in the example above. In the link you gave me, it seems to require the user to input the date manually.
Thanks!
I have done this 2 ways:
a. use %let = variablename (such as year, month day) and then referencing them as macro variables
or
b. use a data step to set the variables and then referencing them as macro variables
Using method a:
%let rundate = %SYSFUNC(today(),yymmddn8.);
%let runhour = %SYSFUNC(HOUR(%SYSFUNC(time(), MINUTE.)), Z2.);
%let runmin = %SYSFUNC(MINUTE(%SYSFUNC(TIME(), MINUTE.)), Z2.);
then set variable to use the above values:
%let exportfile="REPORT_as_of_&rundate._&runhour.&runmin..xml";
Note the extra period after each macro variable when it is referenced using &.
OR
Using method b:
Create a table that gets the "rundate":
PROC SQL;
CREATE TABLE WORK.GET_REPORT_RUNDATE AS
SELECT DISTINCT /* REPORT_RUNDATE */
(DATETIME()) FORMAT=NLDATM30. AS REPORT_RUNDATE
FROM WORK.GET_ALL_TESTS t1;
QUIT;
Then use a data step to break apart the datetime:
data temp;
set work.get_report_rundate;
rundate = datepart(report_rundate);
year = year(rundate);
month = month(rundate);
runtime = timepart(report_rundate);
hour = hour(runtime);
minute = minute(runtime);
format month z2.;
format hour z2.;
format minute z2.;
charmonth = put(month, z2.);
charhour = put(hour, z2.);
charmin = put(minute, z2.);
reportrundate = year||charmonth||'_'||charhour||charmin;
run;
Then, reference those macro variables :
%let exportfile="REPORT_as_of_&rundate._&runhour.&runmin..xml";
NewVar = mdy(monthvar,dayvar,yearvar);
format Newvar mmddyyd10.;
Thanks everyone!!
do you know how I would put it in my code? so far I have
data NEW;
set OLD;
*what command do I put here?* sasdate=mdy(CALL_MONTH,CALL_DAY,CALL_YEAR);
format sasdate mmddyyD10.;
run;
What happens when you run the code that you have?
data NEW;
set OLD;
sasdate=mdy(CALL_MONTH,CALL_DAY,CALL_YEAR);
format sasdate mmddyyD10.;
run;
When I run a proc freq on sasdate, it says all the frequencies are missing.
What do you get when you run proc freq on CALL_MONTH,CALL_DAY and CALL_YEAR?
Convert each of the fields to a number:
sasdate=mdy(input(left(CALL_MONTH),2.),input(left(CALL_DAY),2.),input(left(CALL_YEAR),4.));
If the columns are no wider than the numbers then you don't need the left function.
Hi Arthur,
I got frequencies when I ran the variables individually.
Thanks!
I figured out what I was doing wrong...I was using the wrong data set to run the proc freq. Sorry about the silly mistake. It works now..thanks everybody for your help!!!
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.
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.