DATA Step, Macro, Functions and more

Is there one way to drop last variable always?

Accepted Solution Solved
Reply
Contributor
Posts: 38
Accepted Solution

Is there one way to drop last variable always?

I want to drop last variable but i dont know how.

 

 

Is it possible to do in SAS?


Accepted Solutions
Solution
‎11-17-2017 06:17 PM
PROC Star
Posts: 1,192

Re: Is there one way to drop last variable always?

[ Edited ]

There is probably a smarter way, but you could do something like this

 

data have;
input var1-var3;
datalines;
1 2 3
4 5 6
7 8 9
;

ods select Variables;
proc contents data=have;
	ods output Variables=var;
run;

proc sort data=var;
	by Num;
run;

data _null_;
	set var;
	by num;
	if last.num then call symputx('lastvar', Variable);
run;

%put The last variable is &lastvar;

data want;
	set have;
	drop &lastvar;
run;

View solution in original post


All Replies
Trusted Advisor
Posts: 1,827

Re: Is there one way to drop last variable always?

What do you mean by last variable?

Can you post a sample of data and point at what do you wand to drop ?!

Contributor
Posts: 38

Re: Is there one way to drop last variable always?

 

data matriz;
INPUT x1-x3 var @@;
datalines;
20	6	14	1
21	6	18	2
20	6	14	3
24	6	45	5
;
run;

If we have this code i want to drop variable named "var". but i can have more variables in the future and i want drop last variable always, because his position is always the last one.

 

 

 

Super User
Posts: 9,611

Re: Is there one way to drop last variable always?

Retrieve the information from dictionary.columns, and store into a macro variable used for dropping:

data matriz;
INPUT x1-x3 var;
datalines;
20 6 14 1
21 6 18 2
20 6 14 3
24 6 45 5
;
run;

proc sql noprint;
select name into:lastvar
from dictionary.columns
where libname='WORK' and memname='MATRIZ'
having varnum=max(varnum)
;
quit;

%put &lastvar; * for control purposes;

data matriz_new;
set matriz;
drop &lastvar;
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Solution
‎11-17-2017 06:17 PM
PROC Star
Posts: 1,192

Re: Is there one way to drop last variable always?

[ Edited ]

There is probably a smarter way, but you could do something like this

 

data have;
input var1-var3;
datalines;
1 2 3
4 5 6
7 8 9
;

ods select Variables;
proc contents data=have;
	ods output Variables=var;
run;

proc sort data=var;
	by Num;
run;

data _null_;
	set var;
	by num;
	if last.num then call symputx('lastvar', Variable);
run;

%put The last variable is &lastvar;

data want;
	set have;
	drop &lastvar;
run;
Contributor
Posts: 38

Re: Is there one way to drop last variable always?

[ Edited ]

Yes it works fine but i´m using this code and it always put 0

 

proc sql;
    select  count (varnum) into :numvar
    from    dictionary.COLUMNS
	 where upcase(LIBNAME) =  "WORK"  and
     upcase(MEMNAME) = "matriz";
quit;

 

First i´m looking for number of vars and i will delete it after. 

PROC Star
Posts: 1,192

Re: Is there one way to drop last variable always?

[ Edited ]

1. this code returns zero because you use the UPCASE Function and do not upcase the string "matriz". It should be

 

proc sql;
    select  count (varnum) into :numvar
    from    dictionary.COLUMNS
	 where upcase(LIBNAME) =  "WORK"  and
     upcase(MEMNAME) = "MATRIZ";
quit;

2. Why? This code will give you the number of colums in your data set. How do you go from there to actually dropping the numvar'th variable?

 

Super User
Super User
Posts: 9,227

Re: Is there one way to drop last variable always?

Out of interest, what is the logical reason for always removing the last variable?  It sounds a bit like your doing some processing on lots of datasets and then trying to fix it later on.  Its highly likely that a simple change to the process would remove the need for this at all.

Contributor
Posts: 38

Re: Is there one way to drop last variable always?

Problaby you are right. I need to save last variable in other dataset.
Super User
Posts: 10,623

Re: Is there one way to drop last variable always?

data class;
 set sashelp.class;
run;

proc transpose data=class(obs=0) out=temp;
var _all_;
run;
data _null_;
 set temp point=nobs nobs=nobs;
 call execute(cat('data want;set class;drop ',_name_,';run;'));
 stop;
run;
Frequent Contributor
Posts: 84

Re: Is there one way to drop last variable always?

How weight variable deleted? As per my understanding 

 

The NOBS = option creates a variable which contains the total number of observations in the input data set(s). If multiple data sets are listed in the SET statement, the value in the NOBS = variable are the total number of observations in all the listed data sets.

 

This is read in online. Can some one explain it...

Super User
Posts: 9,611

Re: Is there one way to drop last variable always?

Posted in reply to rajeshalwayswel

Proc transpose (when used in this way) creates a dataset with observations (rows) for columns, so the last observation will contain the name of the last variable in _name_.

The point=nobs nobs=nobs option combination will read exactly this last observation from the transposed dataset.

Note that @Ksharp's solution reads the dataset twice (and additionally writes the transposed dataset), while mine (using the table metadata) only needs one read of the complete dataset. I guess it's just to show how many ways one can find in SAS to solve a specific issue.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Frequent Contributor
Posts: 84

Re: Is there one way to drop last variable always?

Posted in reply to KurtBremser

Thank you... and one more thing in your code SQl "select" statement I seen name variable but it not found in source data set How it going to work...(I'm seen X1-X3 and var variable's)

PROC Star
Posts: 1,192

Re: Is there one way to drop last variable always?

[ Edited ]
Posted in reply to rajeshalwayswel

Check this out Smiley Happy

 

How to View Dictionary Tables

 

 

 

 

Super User
Posts: 9,611

Re: Is there one way to drop last variable always?

Posted in reply to rajeshalwayswel

rajeshalwayswel wrote:

Thank you... and one more thing in your code SQl "select" statement I seen name variable but it not found in source data set How it going to work...(I'm seen X1-X3 and var variable's)


Look closely. The SQL reads from dictionary.columns, which is a dynamically populated meta-dataset. Google for "sas dictionary tables" to see what you can get from them.

 

Although syntactically not reading from the source dataset, technically it reads the file header and retrieves the dataset's structural information.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 14 replies
  • 273 views
  • 4 likes
  • 7 in conversation