[英]Dropping columns based on the column number - SAS
I have a dataset containing the following columns: 我有一个包含以下列的数据集:
ID, value, value1, value2
I want to keep columns ID
and value
. 我想保留列
ID
和value
。 I want to drop the rest based on numerical position. 我想根据数字位置删除其余部分。 So
value1
and value2
are equivalent to 3 and 4. I want to drop anything greater than or equal to 3. 所以
value1
和value2
等于3和4。我想删除大于或等于3的任何东西。
Is there any syntax in the SAS/SQL repertoire that will allow me to reference and drop the columns based on their position. SAS / SQL库中是否有任何语法可以让我根据列的位置引用和删除列。
DATA TEST;
INPUT id $ value value1 value2;
DATALINES;
a 10 1 8
a 13 2 11
a 14 3 23
b 15 4 44
b 44 5 45
c 64 6 67
c 32 6 47
d 12 7 895
;
RUN;
Thanks. 谢谢。
One way would be to read the columns from sashelp.vcolumn
, then save the first 4 columns into a macro variable. 一种方法是从
sashelp.vcolumn
读取列,然后将前4列保存到宏变量中。
proc sql inobs=4;
select name
into :keepcols separated by ','
from TEST
where libname = 'WORK'
AND memname = 'TEST';
proc sql;
select &keepcols
from TEST;
quit;
If you cannot use the dictionary table for some reason, you can use proc transpose
to get the columns: 如果由于某种原因而无法使用字典表,则可以使用
proc transpose
来获取列:
proc transpose data=TEST(obs=0)
out=cols;
run;
proc sql noprint inobs=4;
select _NAME_
into :keepcols separated by ','
from cols;
quit;
proc sql;
select &keepcols
from TEST;
quit;
Proc Contents is often quicker to execute than the dictionary tables. Proc Contents通常比字典表执行得更快。
PROC CONTENTS DATA=sourcetable OUT=temptable NOPRINT ;
RUN ;
PROC SQL ;
SELECT name
INTO :selectclause SEPARATED BY ', '
FROM temptable
WHERE varnum LE 3
ORDER BY npos
;
CREATE TABLE shorttable AS
SELECT &selectclause
FROM sourcetable
;
QUIT ;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.