Help using Base SAS procedures

Combining variables into one output

Reply
Occasional Contributor
Posts: 14

Combining variables into one output

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

PROC Star
Posts: 7,366

Re: Combining variables into one output

Occasional Contributor
Posts: 14

Re: Combining variables into one output

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!

N/A
Posts: 1

Re: Combining variables into one output

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

Occasional Contributor
Posts: 12

Re: Combining variables into one output


NewVar = mdy(monthvar,dayvar,yearvar);

format Newvar mmddyyd10.;

Occasional Contributor
Posts: 14

Re: Combining variables into one output

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;

PROC Star
Posts: 7,366

Re: Combining variables into one output

What happens when you run the code that you have?

Occasional Contributor
Posts: 12

Re: Combining variables into one output

data NEW;

set OLD;

sasdate=mdy(CALL_MONTH,CALL_DAY,CALL_YEAR);

format sasdate mmddyyD10.;

run;

Occasional Contributor
Posts: 14

Re: Combining variables into one output

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

PROC Star
Posts: 7,366

Re: Combining variables into one output

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

Occasional Contributor
Posts: 12

Re: Combining variables into one output


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.

Occasional Contributor
Posts: 14

Re: Combining variables into one output

Hi Arthur,

I got frequencies when I ran the variables individually.

Thanks!

Occasional Contributor
Posts: 14

Re: Combining variables into one output

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

Ask a Question
Discussion stats
  • 12 replies
  • 412 views
  • 0 likes
  • 4 in conversation