BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
michtka
Fluorite | Level 6

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

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

7 REPLIES 7
LinusH
Tourmaline | Level 20

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
Astounding
PROC Star

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.

michtka
Fluorite | Level 6

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

michtka
Fluorite | Level 6

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

Astounding
PROC Star

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.

michtka
Fluorite | Level 6

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.

Ksharp
Super User

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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