BookmarkSubscribeRSS Feed
summerkk
Calcite | Level 5

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!!

12 REPLIES 12
summerkk
Calcite | Level 5

Thanks! Bear with me, I am new to sas:

I did see this on another thread:

1. how to convert char var to sas date?

Hai.kuoMaster

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!

george_proc_sql
Calcite | Level 5

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";

nathan_owens
Obsidian | Level 7


NewVar = mdy(monthvar,dayvar,yearvar);

format Newvar mmddyyd10.;

summerkk
Calcite | Level 5

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;

art297
Opal | Level 21

What happens when you run the code that you have?

nathan_owens
Obsidian | Level 7

data NEW;

set OLD;

sasdate=mdy(CALL_MONTH,CALL_DAY,CALL_YEAR);

format sasdate mmddyyD10.;

run;

summerkk
Calcite | Level 5

When I run a proc freq on sasdate, it says all the frequencies are missing.

art297
Opal | Level 21

What do you get when you run proc freq on CALL_MONTH,CALL_DAY and CALL_YEAR?

nathan_owens
Obsidian | Level 7


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.

summerkk
Calcite | Level 5

Hi Arthur,

I got frequencies when I ran the variables individually.

Thanks!

summerkk
Calcite | Level 5

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!!!

sas-innovate-2024.png

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.

 

Register now!

What is Bayesian Analysis?

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 12 replies
  • 1558 views
  • 0 likes
  • 4 in conversation