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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 6837 views
  • 1 like
  • 4 in conversation