Help using Base SAS procedures

how to rearrange the column position in sql proceure

Reply
Regular Contributor
Posts: 222

how to rearrange the column position in sql proceure

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;

Super User
Posts: 19,877

Re: how to rearrange the column position in sql proceure

Posted in reply to Mike_Davis

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

Super User
Super User
Posts: 7,080

Re: how to rearrange the column position in sql proceure

Posted in reply to Mike_Davis

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;

Regular Contributor
Posts: 184

Re: how to rearrange the column position in sql proceure

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;

Valued Guide
Posts: 765

Re: how to rearrange the column position in sql proceure

Posted in reply to Mike_Davis

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;

Super User
Super User
Posts: 7,080

Re: how to rearrange the column position in sql proceure

But the keep option will not change the order of the variables.  So in this case CITY ends up before STATE.

Regular Contributor
Posts: 222

Re: how to rearrange the column position in sql proceure

Could this be solved by using proc iml?

Respected Advisor
Posts: 4,936

Re: how to rearrange the column position in sql proceure

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

PG
Valued Guide
Posts: 765

Re: how to rearrange the column position in sql proceure

Posted in reply to Mike_Davis

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;

Regular Contributor
Posts: 222

Re: how to rearrange the column position in sql proceure


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.

Super Contributor
Posts: 1,636

Re: how to rearrange the column position in sql proceure

Posted in reply to Mike_Davis

Your statement "retain statement still need us list each variable" is not true. You only need to list "state" and "city" in retain statement.

Valued Guide
Posts: 765

Re: how to rearrange the column position in sql proceure

Hi actually, you only have to list STATE (I edited my posting !!!).

Regular Contributor
Posts: 222

Re: how to rearrange the column position in sql proceure

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.

Respected Advisor
Posts: 3,799

Re: how to rearrange the column position in sql proceure

Posted in reply to Mike_Davis

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?

Regular Contributor
Posts: 222

Re: how to rearrange the column position in sql proceure

Posted in reply to data_null__

like use:

proc sql;

select 5th_variable--20th_variable, 2nd_variable

from..

;

...

Ask a Question
Discussion stats
  • 15 replies
  • 4262 views
  • 7 likes
  • 9 in conversation