Help using Base SAS procedures

add extra record heading the dataset

Accepted Solution Solved
Reply
Super Contributor
Posts: 301
Accepted Solution

add extra record heading the dataset

Hi guys, I want to add this col0 to the top of the dataset "have", i.e  header this dataset,

but with using this proc sql code the record will be add to the bottom, any help to put it

as the header of the table rather than the bottom?

proc sql;

*Add new record;

insert into have

set col0='Race'

;

quit;

Thanks,

V


Accepted Solutions
Solution
‎06-20-2012 09:55 AM
Super User
Posts: 5,513

Re: add extra record heading the dataset

SQL isn't necessarily the right tool to solve every problem.  Here's another approach:

data header_record;

if 0 then set have;  /* define identical variable attributes */

col0 = 'Race';

output;

stop;

run;

proc append data=have base=header_record;

run;

Good luck.

View solution in original post


All Replies
Super User
Posts: 5,432

Re: add extra record heading the dataset

Why do you want it on the top?

resorting the table can help. If you always want the newest at the top, add a load time column, and sort it by descending.

Data never sleeps
Solution
‎06-20-2012 09:55 AM
Super User
Posts: 5,513

Re: add extra record heading the dataset

SQL isn't necessarily the right tool to solve every problem.  Here's another approach:

data header_record;

if 0 then set have;  /* define identical variable attributes */

col0 = 'Race';

output;

stop;

run;

proc append data=have base=header_record;

run;

Good luck.

Super Contributor
Posts: 301

Re: add extra record heading the dataset

Posted in reply to Astounding

I know, for this reason I ask.

A question, which difference show your code with this other one?

                   data new0;
                    length col0 $30;
                    col0='Race';
                    run;

                   data newhave;
                   set new0 have;
                   run;

Thanks.

V

Super Contributor
Posts: 301

Re: add extra record heading the dataset

sorry, this one...to compare with yours:

                   data header_record;
                    length col0 $30;
                    col0='Race';
                    run;

                   data have;
                   set header_record have;
                   run;

Thanks.

V

Super User
Posts: 5,513

Re: add extra record heading the dataset

They all work (assuming $30 is the right length for col0).

PROC APPEND will run slightly faster than the DATA step version.  But that may not be an issue.  In fact it has the downside feature of less flexibility in naming the output data set.

Your DATA step variations require prior knowledge of the proper length for COL0.  To get around that requirement, I used the more complex DATA step to create HEADER_RECORD.  Even if you stick with your most recent approach, I would still recommend using my version of the DATA step that creates HEADER_RECORD.  It eliminates the chance that you'll make a mistake when setting a length for COL0.  The best reason for using the LENGTH statement approach is if the length for COL0 in HAVE is too short and you wish to set a longer length.

Good luck.

Super Contributor
Posts: 301

Re: add extra record heading the dataset

Posted in reply to Astounding

Completelly agree, thank you for the explanation, it is exactly I wanted to understand to avoid a possible truncation issue for not considering the right length for the col0 variable.

Super User
Posts: 10,041

Re: add extra record heading the dataset

Or a SQL alternative approach .

proc sql;
create table want as
 select 'Patrick' as Name length=20 from sashelp.class(obs=1)
  outer union corresponding
 select * from sashelp.class;
quit;


Ksharp

🔒 This topic is solved and locked.

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

Discussion stats
  • 7 replies
  • 234 views
  • 0 likes
  • 4 in conversation