BookmarkSubscribeRSS Feed
StephanDup
Fluorite | Level 6

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;
3 REPLIES 3
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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; 

 

Ksharp
Super User
Since your matrix refer to alll numeric variables. Try

drop _numeric_;

Astounding
PROC Star

@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.)

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 8288 views
  • 1 like
  • 4 in conversation