DATA Step, Macro, Functions and more

SAS ARRAY - DATE FORMATS

Reply
Contributor
Posts: 60

SAS ARRAY - DATE FORMATS

 Hi,

 

I have the below code whereby some of my variables that I specify to be dates don't show in my output field.

 

How can I format STARTDT and ENTERDT to be date9. fields in my output?

 

 

DATA DEPOSITS_ARRAY_BASE;
ARRAY RESORT 		{3} $500.;
ARRAY INTERV 		{3} $500.;
ARRAY STARTDT 		{3} DATE9.;
ARRAY ENTERDT		{3} DATE9.;
ARRAY REGION		{3} $500.;
ARRAY CHANNEL		{3} $500.;

FLAG=0;
DO UNTIL (LAST.SBR_ID);
SET DEPOSITS;
BY SBR_ID;
FLAG + 1;
IF FLAG <= 3 THEN DO; RESORT{FLAG} = RESORT_ID;		END;
IF FLAG <= 3 THEN DO; INTERV{FLAG} = INTERVAL;		END;
IF FLAG <= 3 THEN DO; STARTDT{FLAG} = START_DT;		END;
IF FLAG <= 3 THEN DO; ENTERDT{FLAG} = ENTER_DT;		END;
IF FLAG <= 3 THEN DO; REGION{FLAG} = PRIM_REGION;	END;
IF FLAG <= 3 THEN DO; CHANNEL{FLAG} = ENTRY_OPR_ID;	END;
END;
DROP 	FLAG ROW SPB_TYPE SPB_STAT XCH_SPB_REL_NO 
RESORT_ID INTERVAL START_DT ENTER_DT PRIM_REGION ENTRY_OPR_ID; RUN;

 

 

 

 

Super User
Posts: 7,760

Re: SAS ARRAY - DATE FORMATS

Posted in reply to CamRutherford

You can only assign lengths, not formats in an array definition. Use a separate format statement like in

data have;
x1 = today();
output;
x1 = today() + 1;
output;
x1 = today() +2 ;
output;
run;

data want;
array dates{3} 4;
format dates1-dates3 date9.;
do i = 1 to 3;
  set have;
  dates{i} = x1;
end;
drop i x1;
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Trusted Advisor
Posts: 1,553

Re: SAS ARRAY - DATE FORMATS

Posted in reply to KurtBremser

The next part of your code:

FLAG + 1;
IF FLAG <= 3 THEN DO; RESORT{FLAG} = RESORT_ID;		END;
IF FLAG <= 3 THEN DO; INTERV{FLAG} = INTERVAL;		END;
IF FLAG <= 3 THEN DO; STARTDT{FLAG} = START_DT;		END;
IF FLAG <= 3 THEN DO; ENTERDT{FLAG} = ENTER_DT;		END;
IF FLAG <= 3 THEN DO; REGION{FLAG} = PRIM_REGION;	END;
IF FLAG <= 3 THEN DO; CHANNEL{FLAG} = ENTRY_OPR_ID;	END;
END;

can be replaced into:

FLAG + 1;
IF FLAG <= 3 THEN DO; 
    RESORT{FLAG} = RESORT_ID;
    INTERV{FLAG} = INTERVAL;
    STARTDT{FLAG} = START_DT;	
    ENTERDT{FLAG} = ENTER_DT;	
    REGION{FLAG} = PRIM_REGION;
    CHANNEL{FLAG} = ENTRY_OPR_ID;
END;

having same result with less coding and easier to read.

Contributor
Posts: 60

Re: SAS ARRAY - DATE FORMATS

Posted in reply to CamRutherford

Can I use an array to format numeric values already created?

 

For example, if I wanted an array to format 3 columns, StartDT1 - StartDT3 to be DATE9.

 

Cheers

Super User
Posts: 7,760

Re: SAS ARRAY - DATE FORMATS

Posted in reply to CamRutherford

You need to use the format statement to assign formats.

In your case:

format StartDT1-StartDT3 date9.;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 60

Re: SAS ARRAY - DATE FORMATS

Posted in reply to KurtBremser
Sorry, I don't have a clue when it comes to arrays - your response doesn't make any sense to me, I need to see the whole code to understand
Super User
Posts: 7,760

Re: SAS ARRAY - DATE FORMATS

Posted in reply to CamRutherford

When you assign an array to already existing variables, you must supply the list of variables in the array statement:

array mydates{*} startdate enddate somedate;

You can then use the same list in a format statement:

format startdate enddate somedate date9.;

If you create an arrray and do not supply names for the individual variables, two things can happen

- existing variables that fit the array name(+index) scheme are represented by the array:

data have;
date1 = today();
date2 = date1 + 1;
date3 = date2 + 1;
run;

data want;
set have;
array date{3};
do i = 1 to 3;
  date{i} = date{i} + 1;
end;
drop i;
run;

Note that dataset want will contain the same variables as dataset have.

- if no fitting variables exist, they will be created:

data have;
date1 = today();
date2 = date1 + 1;
date3 = date2 + 1;
run;

data want;
set have;
array dates{3};
do i = 1 to 3;
  dates{i} = date1;
end;
drop i;
run;

Note that dataset want now contains six variables.

So, when you create array dates{*} without naming any variables, you will find that it will contain variable dates1, dates2, dates3 and so on; you can use the list form in a format statement:

%let maxdates=9;

data want;
array dates{&maxdates};
format dates1-dates&maxdates date9.;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Trusted Advisor
Posts: 1,553

Re: SAS ARRAY - DATE FORMATS

Posted in reply to CamRutherford

Next part of your code is wrong:

ARRAY RESORT 		{3} $500.;
ARRAY INTERV 		{3} $500.;
ARRAY STARTDT 		{3} DATE9.;
ARRAY ENTERDT		{3} DATE9.;
ARRAY REGION		{3} $500.;
ARRAY CHANNEL		{3} $500.;

ARRAY statement is a tool to assign a name to a list of variables, of same type,

in order to enable relate to an array member (a speceific variable) by index.

 

ARRAY is not the tool to assign formats to variables.

 

Example:

     array varx {3} var1 var2 var3;  /* can be shorten to var1-var3 */

in this example VARX is the name of the array combined of 3 variables.

You relate to the second variable in the array, either by VAR2 or by VARX(2);

 

If you want to assing formats to those variables use the FORMAT statement:

    format var1-var3 comma6.2;   /* or any other format */

 

Super User
Super User
Posts: 7,039

Re: SAS ARRAY - DATE FORMATS

[ Edited ]
Posted in reply to CamRutherford

A format is a tool for telling SAS how to display the values of the variable.  It is NOT how you define a variable. A SAS variable is either a fixed length character string or a floating point number.  In an ARRAY statement you can optionally define the type and length of the variables, but there is no way to attach a format to the variables using the ARRAY statement.  To attach a format you need use a FORMAT or ATTRIB statement.

 

Note that when specifing a variable's length you do not need to include a period.  Periods are used when specifying a format so that the parser can distinquish them from variable names or other keywords.  SAS will silently ignore the extra periods that you had included after the lengths in your ARRAY statements.

 

proc sql noprint;
  select max(nobs) into :max_obs trimmed
  from (select sbr_id,count(*) as nobs from deposits group by 1)
  ;
quit;

data deposits_array_base;
  array resort (&max_obs) $500;
  array interv (&max_obs) $500;
  array startdt (&max_obs) 8;
  array enterdt (&max_obs) 8;
  array region (&max_obs) $500;
  array channel (&max_obs) $500;
  format startdt: enterdt: date9. ;
  do flag=1 to &max_obs until (last.sbr_id);
    set deposits;
    by sbr_id;
    resort(flag) = resort_id;
    interv(flag) = interval;
    startdt(flag) = start_dt;
    enterdt(flag) = enter_dt;
    region(flag) = prim_region;
    channel(flag) = entry_opr_id;
  end;
  drop flag row spb_type spb_stat xch_spb_rel_no resort_id interval
       start_dt enter_dt prim_region entry_opr_id
  ;
run;

 

Ask a Question
Discussion stats
  • 8 replies
  • 654 views
  • 0 likes
  • 4 in conversation