- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi all,
I have a problem when I try to create a new dataset,change state and city as first and second columns and then keep column from statename to dst,but there is problem in the code:
please help.
Thanks!
proc sql noprint;
create table one as
select a.state
,a.city
,a.statename--a.dst
from sashelp.zipcode as a
;
quit;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
There isn't an easy way to short cut variable selections for SQL procedures.
What I typically do, if I absolutely require the order is run the proc sql with a select * and the feedback option. This writes the full query to the log and then I modify that as required.
Saves typing but the code isn't as succinct as possible.
12 proc sql noprint feedback;
13
14 create table one as
15
16 select *
17
18 from sashelp.zipcode as a
19
20 ;
NOTE: Statement transforms to:
select A.ZIP, A.Y, A.X, A.ZIP_CLASS, A.CITY, A.STATE, A.STATECODE, A.STATENAME, A.COUNTY, A.COUNTYNM,
A.MSA, A.AREACODE, A.AREACODES, A.TIMEZONE, A.GMTOFFSET, A.DST, A.PONAME, A.ALIAS_CITY
from SASHELP.ZIPCODE A;
NOTE: Table WORK.ONE created, with 41761 rows and 18 columns.
21
22 quit;
NOTE: PROCEDURE SQL used (Total process time):
real time 0.14 seconds
cpu time 0.09 seconds
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Here is a useful trick for getting variable lists expanded. Use PROC TRANSPOSE to make a dataset with the variable names.
You can then pull the list into a macro variable.
proc transpose data=sashelp.zipcode(obs=0) out=_names_;
var state city statename--dst ;
run;
proc sql noprint ;
select _name_ into :varlist separated by ', ' from _names_;
create table one as select &varlist from sashelp.zipcode ;
quit;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Or, avoid the PROC TRANSPOSE step by letting the ORDER BY clause and the KEEP= option do the work.
select name
into :varlist separated by ', '
from dictionary.columns
where libname EQ 'SASHELP' and memname EQ 'ZIPCODE'
order by name NE 'STATE', name NE 'CITY', varnum ;
create table one(keep = state city statename--dst) as
select &varlist
from sashelp.zipcode ;
Tom wrote:
Here is a useful trick for getting variable lists expanded. Use PROC TRANSPOSE to make a dataset with the variable names.
You can then pull the list into a macro variable.
proc transpose data=sashelp.zipcode(obs=0) out=_names_;
var state city statename--dst ;
run;
proc sql noprint ;
select _name_ into :varlist separated by ', ' from _names_;
create table one as select &varlist from sashelp.zipcode ;
quit;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
hi ... the '--' works as a data set option, so (no NOPRINT needed when you create a table, no table alias A needed either) ...
proc sql;
create table one as
select *
from sashelp.zipcode (keep=state city statename--dst);
quit;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
But the keep option will not change the order of the variables. So in this case CITY ends up before STATE.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Could this be solved by using proc iml?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
If you don't mind the warnings, you can use:
proc sql;
create table one as
select state, city, *
from sashelp.zipcode(keep=state city statename--dst);
quit;
not very clean, but quick.
PG
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
hi ... since no one else has asked, do you have to use SQL ...
data one;
retain state;
set sashelp.zipcode (keep=state city statename--dst);
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thank you Mike,
This is a good method while I prefer it will be in sql or IML procedure,
retain statement still need us list each variable which we want to rearrange.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Your statement "retain statement still need us list each variable" is not true. You only need to list "state" and "city" in retain statement.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi actually, you only have to list STATE (I edited my posting !!!).
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Sorry for I didn't express my opinon clearly,I have revised it:
retain statement still need us list each variable which we want to rearrange.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Mike.Davis wrote:
Sorry for I didn't express my opinon clearly,I have revised it:
retain statement still need us list each variable which we want to rearrange.
How can you rearrange the variables without referring to them?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
like use:
proc sql;
select 5th_variable--20th_variable, 2nd_variable
from..
;
...