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

I have two columns listed below..

Header 1
2
3
4
5
6

Header 1
1
2
5

And I want to create something like this below..

Header 1
1
2
3
4
5
6

Can I do this in SAS data step?

1 ACCEPTED SOLUTION

Accepted Solutions
Steelers_In_DC
Barite | Level 11

You will have to rename, using either way will look like this:

data want;

update have1 have2(rename=(header_two=header));

by header;

run;

proc sql;

create table want as

select header from have1

union

select header_two as header from have2

order by header;

View solution in original post

8 REPLIES 8
ballardw
Super User

If the two sets are sorted by the Header variable:

data want;

     merge dataset1 dataset2;

     by Header1;

run;

If you are combining sets with more variables with the same name then the order the datasets appear on the Merge statement become an issue as will if the BY variable has repeated values in both datasets.

JUN_Sands
Calcite | Level 5

I should have mentioned that two columns have different labels. Do I need to change them to same column name before merge them?

Steelers_In_DC
Barite | Level 11

You will have to rename, using either way will look like this:

data want;

update have1 have2(rename=(header_two=header));

by header;

run;

proc sql;

create table want as

select header from have1

union

select header_two as header from have2

order by header;

ballardw
Super User

If you have any doubt add a label statement to the code. By default variable characteristics such as Label or Format will be the one associated with the first listed data set for those items common to more than one set.

slchen
Lapis Lazuli | Level 10

You could use merge or update statement

Steelers_In_DC
Barite | Level 11

Union will eliminate the duplicates and you can sort in the same step:

data have1;

infile cards dsd dlm=',';

input header;

cards;

2

3

4

5

6

;

run;

data have2;

infile cards dsd dlm=',';

input header;

cards;

1

2

5

;

run;

proc sql;

create table want as

select header from have1

union

select header from have2

order by header;

Steelers_In_DC
Barite | Level 11

If I was doing this using datastep I would do the following:

data want;

set have1 have2;

run;

proc sort data=want nodup;by header;

Steelers_In_DC
Barite | Level 11

also:

data want;

update have1 have2;

by header;

run;

Update can get a little trickier.  You might want to read up on that to get a better understanding if you use it.

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 8 replies
  • 1414 views
  • 6 likes
  • 4 in conversation