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

Hi all, 

 

I have two datasets named have1 and have2

 

Both datasets have 4 columns:

x1 x2 x3 c4

while x1,x3 are numeric and c is character. Now I only want to concatenate these two datasets without column x2 (because it was numeric in dataset have 1 but character in have2), I mean the concatenated dataset is

x1 x3 c4

My code to do concatenate in normal condition (without dropping x2) is:

data concatenate_;
length x1 8 x2 8 x3 8 c4 $100;
set have1 have2;
run;

Can you please help me to sort it out, thank you! Or in another word, how to concatenate two files with only some specific columns?

Warm regards.

 

Thank you for your help, have a fabulous and productive day! I am a novice today, but someday when I accumulate enough knowledge, I can help others in my capacity.
1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

Below some options

data concatenate_;
length x1 8 x3 8 c4 $100;
set have1(keep=x1 x3 c4) have2 (keep=x1 x3 c4);
run;

Or this way:

proc sql;
  create table combined as
  select x1,x3,c4
  from have1
  union all corr
  select x1,x3,c4
  from have2
  ;
quit;

Or if you want to keep x2 as character (in example below assumed numeric in have2)

proc sql;
  create table combined as
  select x1,x3,c4, x2
  from have1
  union all corr
  select x1,x3,c4, put(x2, 32.) as x2 length=16
  from have2
  ;
quit;

or you could also convert the character to numeric if the data allow for it

proc sql;
  create table combined as
  select x1,x3,c4, input(x2, best32.) as x2
  from have1
  union all corr
  select x1,x3,c4, x2
  from have2
  ;
quit;

and to add to above: Using a SAS UNION will for character variables in the target table use the max length of the variable coming from the sources so you never risk data truncation if lengths differ.

View solution in original post

3 REPLIES 3
SASKiwi
PROC Star
data concatenate_ (keep = x1 x3 c4);
length x1 8 x2 8 x3 8 c4 $100;
set have1 have2;
run;
Phil_NZ
Barite | Level 11

Hi @SASKiwi 

I see what you mean. Sorry I did not explain well. What I want is I do not want SAS to perform concatenating on x2 (because the types of x2 are mixed: it was numeric in have1 but character in have2)..

Thank you!

Thank you for your help, have a fabulous and productive day! I am a novice today, but someday when I accumulate enough knowledge, I can help others in my capacity.
Patrick
Opal | Level 21

Below some options

data concatenate_;
length x1 8 x3 8 c4 $100;
set have1(keep=x1 x3 c4) have2 (keep=x1 x3 c4);
run;

Or this way:

proc sql;
  create table combined as
  select x1,x3,c4
  from have1
  union all corr
  select x1,x3,c4
  from have2
  ;
quit;

Or if you want to keep x2 as character (in example below assumed numeric in have2)

proc sql;
  create table combined as
  select x1,x3,c4, x2
  from have1
  union all corr
  select x1,x3,c4, put(x2, 32.) as x2 length=16
  from have2
  ;
quit;

or you could also convert the character to numeric if the data allow for it

proc sql;
  create table combined as
  select x1,x3,c4, input(x2, best32.) as x2
  from have1
  union all corr
  select x1,x3,c4, x2
  from have2
  ;
quit;

and to add to above: Using a SAS UNION will for character variables in the target table use the max length of the variable coming from the sources so you never risk data truncation if lengths differ.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 940 views
  • 2 likes
  • 3 in conversation