DATA Step, Macro, Functions and more

How to combine two columns?

Accepted Solution Solved
Reply
Super Contributor
Posts: 319
Accepted Solution

How to combine two columns?

Hello:

 

I would like to combine column 'Seq' and 'lab_mom' together.  Please advice how to do it.   Also, I think there is something wrong with my code cause the column 'lab_child' is not showing the data in SAS.  Thanks.

 

data data3;

infile datalines dlm=',' dsd;

informat ID $4. Seq best8. DOB mmddyy10. lab_mom $8. lab_child $8.;

format dob mmddyys10.;

input ID Seq DOB lab_mom;

datalines;

A045, 1, 05/06/1979, NH,

A045, 2, 05/06/1979, NH,

A045, 1, 05/06/1979, ,NU

B100, 1, 06/15/1982, JJ,

B100, 2, 06/15/1982, JJ,

B100, 1, 06/15/1982, ,PJ

C300, 1, 05/06/1979, KK,

C300, 2, 05/06/1979, KK,

C300, 1, 05/06/1979, ,OL

;

 


Accepted Solutions
Solution
‎08-14-2017 04:35 PM
Super User
Posts: 11,343

Re: How to combine two columns?

[ Edited ]
data data3;
infile datalines dlm=',' dsd; 
informat ID $4. Seq best8. DOB mmddyy10. lab_mom $8. lab_child $8.;
format dob mmddyys10.;
input ID Seq DOB lab_mom;
Combined1= cats(seq,lab_mom);
combined2= cats(lab_mom,seq);
combined3= catx('_',lab_mom,seq);
combined4= catx('-',seq,lab_mom);

datalines;
A045, 1, 05/06/1979, NH,
A045, 2, 05/06/1979, NH,
A045, 1, 05/06/1979, ,NU
B100, 1, 06/15/1982, JJ,
B100, 2, 06/15/1982, JJ,
B100, 1, 06/15/1982, ,PJ
C300, 1, 05/06/1979, KK,
C300, 2, 05/06/1979, KK,
C300, 1, 05/06/1979, ,OL
;


proc print data=data3 noobs;
run;

Your result of 1 is when the is no value for lab_mon. NOTE that you have an extra comma in 3 of your data lines statements in the third, sixth and ninth lines. GIGO.

 

 

If this combined is supposed to be the lab_child then you should say so.

 

And saying the code doesn't work without showing what exact code you ran is pretty difficult to tell which code you mean.

View solution in original post


All Replies
Super User
Posts: 11,343

Re: How to combine two columns?

Since you do not assign any value to Lab_child why to you expect to see one? there is nothing on the dataline to read for a fifth variable. The variable is in the data set.

 

HOW do you want Seg and and Lab_mom combined?

some possible ways:

Combined= cats(seq,lab_mom);

combined= cats(lab_mom,seq);

combined= catx('_',lab_mom,seq);

combined= catx('-',seq,lab_mom);

 

but you really should show what you would expect the combination to look like.

Super Contributor
Posts: 319

Re: How to combine two columns?

Hi, Ballardw:

 

The code is not working.  The result only show numeric number, such as '1.'.   But no charater is appending behind.

Super Contributor
Posts: 319

Re: How to combine two columns?

So, both Cats and Catx work in the numeric and charateric variables?

Super User
Posts: 7,854

Re: How to combine two columns?

[ Edited ]

ybz12003 wrote:

So, both Cats and Catx work in the numeric and charateric variables?


Try it. Look at the results and log afterwards, and learn.
See Maxim 4.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super Contributor
Posts: 319

Re: How to combine two columns?

Such like,

1NH

2NH

1NU

Solution
‎08-14-2017 04:35 PM
Super User
Posts: 11,343

Re: How to combine two columns?

[ Edited ]
data data3;
infile datalines dlm=',' dsd; 
informat ID $4. Seq best8. DOB mmddyy10. lab_mom $8. lab_child $8.;
format dob mmddyys10.;
input ID Seq DOB lab_mom;
Combined1= cats(seq,lab_mom);
combined2= cats(lab_mom,seq);
combined3= catx('_',lab_mom,seq);
combined4= catx('-',seq,lab_mom);

datalines;
A045, 1, 05/06/1979, NH,
A045, 2, 05/06/1979, NH,
A045, 1, 05/06/1979, ,NU
B100, 1, 06/15/1982, JJ,
B100, 2, 06/15/1982, JJ,
B100, 1, 06/15/1982, ,PJ
C300, 1, 05/06/1979, KK,
C300, 2, 05/06/1979, KK,
C300, 1, 05/06/1979, ,OL
;


proc print data=data3 noobs;
run;

Your result of 1 is when the is no value for lab_mon. NOTE that you have an extra comma in 3 of your data lines statements in the third, sixth and ninth lines. GIGO.

 

 

If this combined is supposed to be the lab_child then you should say so.

 

And saying the code doesn't work without showing what exact code you ran is pretty difficult to tell which code you mean.

Super Contributor
Posts: 319

Re: How to combine two columns?

Thank you so much for your great help!

☑ This topic is solved.

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

Discussion stats
  • 7 replies
  • 191 views
  • 1 like
  • 3 in conversation