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;
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;
Since your matrix refer to alll numeric variables. Try drop _numeric_;
@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.)
April 27 – 30 | Gaylord Texan | Grapevine, Texas
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!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.