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

Hi, I have a complicated dataset that contains two different variables that represent the same thing. So I would like to combine both variables together so that the final single variable is all the observations from both variables. I don't want to lose any observations. But for some reason I am having a hard time figuring out how to do this in SAS. Any help would be much appreciated. Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Is this too simple and obvious as a solution?

 

data want;

set have;

cr2=cr;

output;

cr2=cr1;

output;

drop cr cr1;

run;

View solution in original post

14 REPLIES 14
novinosrin
Tourmaline | Level 20

Could you post a sample of what you HAVE and what you WANT to help avoid assumptions

kmardinian
Quartz | Level 8

Yes, here is a simplified example of my dataset

 

ID       CR      CR1      

 x           1           2

 x           4           5

 y           1           3

 z           1           4

 

and I would like the final variable to be

 

ID       CR2

x          1

x          4

x          2

x          5

y          1

y          3

z         1

z         4

 

Thank you!

Reeza
Super User

Did you create the data set? Or is that the starting form?


@kmardinian wrote:

Yes, here is a simplified example of my dataset

 

ID       CR      CR1      

 x           1           2

 x           4           5

 y           1           3

 z           1           4

 

and I would like the final variable to be

 

ID       CR2

x          1

x          4

x          2

x          5

y          1

y          3

z         1

z         4

 

Thank you!




kmardinian
Quartz | Level 8

I created this dataset, it is multiple datasets that have been merged together into one dataset

Reeza
Super User

@kmardinian wrote:

I created this dataset, it is multiple datasets that have been merged together into one dataset


You shouldn't have merged, you should append instead. Go back a step and fix it there instead.

You can use either PROC APPEND or a data step.

 

 

kmardinian
Quartz | Level 8

Sorry, I misspoke, I did not merge the datasets, but set all the datasets into one, because I do not want to lose any observations

 

data t1;

set d1-d10;

Astounding
PROC Star

Is this too simple and obvious as a solution?

 

data want;

set have;

cr2=cr;

output;

cr2=cr1;

output;

drop cr cr1;

run;

kmardinian
Quartz | Level 8

Thank you! This was what I was looking for. I apologize for not having been clear in my previous posts. I'll make sure to include a data step the next time I have a question. Thank you all for your help!

ballardw
Super User

@kmardinian wrote:

Yes, here is a simplified example of my dataset

 

ID       CR      CR1      

 x           1           2

 x           4           5

 y           1           3

 z           1           4

 

and I would like the final variable to be

 

ID       CR2

x          1

x          4

x          2

x          5

y          1

y          3

z         1

z         4

 

Thank you!


Is there a serious reason why this output would not be acceptable?

ID       CR2
x          1
x          2
x          4
x          5
y          1
y          3
z          1
z          4

And if other variables are involved that do not get "moved" then you need to show what a few of them look like before and after

 

Reeza
Super User
How do you want to combine them? Are all combined? In the same way? No cases are different? If not, why not just drop one? We need significantly more details to even start with this question.
Though a random guesses would be COALESCE or CATX.
novinosrin
Tourmaline | Level 20
data have;
input iD  $     CR      CR1      ;
cards;
 x           1           2
 x           4           5
 y           1           3
 z           1           4
 ;

proc transpose data=have out=want(keep= id col1) ;
by id cr cr1;
var cr cr1;
run;
kmardinian
Quartz | Level 8

I have many other variables in the dataset too, would proc transpose mess with them? Thank you!

novinosrin
Tourmaline | Level 20

Unfortunately yes, can you present a better and comprehensive  sample if you want me to work with. I must admit I am not sound at advisory level to foresee and recommend, however if somebody can provide you that, hang in there.

Nevertheless, should you provide best representative samples, i enjoy coding 🙂

mkeintz
PROC Star

I think you can see why it is recommended that a topic originator show a data step with data showing the starting organization (for instance are you showing us the actual layout of, say, data set D1 and D2?  I can't tell yet.   I had assume that @ballardw's suggestion addressed the information as you provided it, but we won't know until you tell us.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 14 replies
  • 12504 views
  • 6 likes
  • 6 in conversation