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

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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