SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

Drop an entire array after using it

Reply
Occasional Contributor
Posts: 8

Drop an entire array after using it

Good day. I have created a lag of all my variables and would like to drop the original variables and only keep the lagged variables.

 

The following code was used unsuccessfully:

data want;
  set have;
  array vars{*} _numeric_;
  array L1vars{1000};
  do i = 1 to dim(vars);
    L1vars{i} = lag(vars{i});
  end;
  drop i;
  drop vars;
 run;
Super User
Super User
Posts: 9,456

Re: Drop an entire array after using it

Posted in reply to StephanDup

You can't drop arrays like that.  Arrays are only temporary references for groups of variables, not elements themselves.  You can of course pre-build the vars:

proc sql;
  select distinct NAME
  into   :V separated by " "
  from   SASHELP.VCOLUMN
  where  LIBNAME="WORK"
    and  MEMNAME="HAVE"
    and  TYPE="num";
quit;
data want;
  set have;
  array vars{*} &V.;
  array L1vars{1000};
  do i = 1 to dim(vars);
    L1vars{i} = lag(vars{i});
  end;
  drop i &V.;
 run; 

 

Super User
Posts: 10,700

Re: Drop an entire array after using it

Posted in reply to StephanDup
Since your matrix refer to alll numeric variables. Try

drop _numeric_;

Super User
Posts: 6,644

Re: Drop an entire array after using it

Posted in reply to StephanDup

@Ksharp gets partial credit here.  It's the right statement to add, but has to be added in exactly the right place.

 

In a DATA step, _numeric_ refers to all numeric variables that have been defined so far.  To get rid of the original numeric variables, the order of the statements must specifically be:

 

array vars {*} _numeric_;

drop _numeric_;

array L1vars .........

 

That's the only point in your DATA step where _numeric_ refers to all the original numeric variables coming from HAVE.  (Yes, technically the DROP statement could come between the SET statement and the first ARRAY statement.)

Ask a Question
Discussion stats
  • 3 replies
  • 410 views
  • 1 like
  • 4 in conversation