consider following codes:
array TestVar
do i = 1 to 3;
drop TestVar;
end;
The purpose of this code is to drop the first three character variable. however, the code does not work for me. how should i edit the code to make it work?
When you use one of the dictionary views in proc sql the libname and memname you specify both have to be in upper case. Thus your line should have read:
where libname='WORK' and memname='TEST'
However, it wouldn't have worked anyway, as the sort would not have been in the expected order and proc sql, as far as I know, doesn't allow numeric collation. Thus one extra step is needed, e.g.:
data have;
retain x1-x100 (100*1);
do i=1 to 2;output;end;
run;
proc sql noprint;
create table names as
select name
from dictionary.columns
where libname='WORK' and
memname='HAVE'
;
quit;
proc sort data=names sortseq=linguistic (numeric_collation=on);
by name;
run;
proc sql noprint;
select name into : list separated by ' '
from names
quit;
data class;
retain &list;
set sashelp.class;
run;
data want;
retain &list;
set have;
run;
You don't need the array. Aren't you just trying to do something like the following?:
data have;
input var 1 (x y z) ($);
cards;
1 1 2 3
2 5 2 1
3 4 3 4
;
data want;
set have;
drop _character_;
run;
Thank you.
"drop _Character_" will drop ALL character variables. I am just wondering if there is some way that can selectively drop some character variables.
I'm not sure how selectively you want. From the pseudo code you had in your initial post, I presumed that it was all character variables.
If you want to drop a range of variables you can always do something like the following, i.e., use a variable list:
data have;
input var 1 (x y z extra) ($);
cards;
1 1 2 3 1
2 5 2 1 2
3 4 3 4 3
;
data want;
set have;
drop x--z;
run;
thank you very much.
I will refrase my quetions: how to reorder variables to be in alphabetical order?
For example: supppose the original variables are (in order of):
C100 B100 A100 C99 B99 A99 C98 B98 A98 ...... C1 B1 A1
How can I change the order to:
A1 A2 ... A100 B1 B2 ... B100 C1 C2 .. C100
Drop is not executable. You will need to build the list first then use it in data step or data set option. Here is one way to do it.
25 proc transpose data=sashelp.heart(obs=0) out=vars;
26 var _char_;
27 run;
NOTE: There were 0 observations read from the data set SASHELP.HEART.
NOTE: The data set WORK.VARS has 7 observations and 2 variables.
NOTE: PROCEDURE TRANSPOSE used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds
28 proc sql noprint;
29 select _name_ into :drop3 separated by ' '
30 from vars(obs=3);
31 quit;
NOTE: PROCEDURE SQL used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
32 run;
33 %put NOTE: DROP3=&drop3;
NOTE: DROP3=Status DeathCause Sex
How about:
proc sql noprint; select name into : list separated by ' ' from dictionary.columns where libname='SASHELP' and memname='CLASS' order by name; quit; data class; retain &list; set sashelp.class; run;
Ksharp
thank you. I believe this is the answer i am looking for. but somehow i couldn't make it work:
data test;
input c2 b2 a2 c1 b1 a1;
cards;
1 2 3 4 5 6
;
proc sql noprint;
select name into : list separated by ' '
from dictionary.columns
where libname='work' and memname='test'
order by name;
quit;
data new;
retain &list;
set test;
run;
where am i wrong?
When you use one of the dictionary views in proc sql the libname and memname you specify both have to be in upper case. Thus your line should have read:
where libname='WORK' and memname='TEST'
However, it wouldn't have worked anyway, as the sort would not have been in the expected order and proc sql, as far as I know, doesn't allow numeric collation. Thus one extra step is needed, e.g.:
data have;
retain x1-x100 (100*1);
do i=1 to 2;output;end;
run;
proc sql noprint;
create table names as
select name
from dictionary.columns
where libname='WORK' and
memname='HAVE'
;
quit;
proc sort data=names sortseq=linguistic (numeric_collation=on);
by name;
run;
proc sql noprint;
select name into : list separated by ' '
from names
quit;
data class;
retain &list;
set sashelp.class;
run;
data want;
retain &list;
set have;
run;
The code works now,
Thank you so much, art297 and Ksharp. I really learned a lot from both of you.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.