DATA Step, Macro, Functions and more

merge variables from save file to new variable and include in same file

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 78
Accepted Solution

merge variables from save file to new variable and include in same file

I'm attemptine to merge several variables together and create a new merged variable for plotting. The  simple concept is below.

 

data have;

set have;

merge varr1 var2 ... into varnew;

run';

 

proc sgplot data=have;

vbar varnew;

run;


Accepted Solutions
Solution
‎10-23-2017 03:57 PM
Super User
Posts: 23,928

Re: merge variables from save file to new variable and include in same file

New_Var = CoalesceC(var1, var2, var3);

View solution in original post


All Replies
Frequent Contributor
Posts: 78

Re: merge variables from save file to new variable and include in same file

The var1 var2 etc have the same format and length.

Super User
Posts: 23,928

Re: merge variables from save file to new variable and include in same file

merging data sets uses a MERGE statement. 
If you're trying to combine variables use a CAT type formula. 

 

CATX() will concatenate variables with a delimiter, CATT() is another option.

Frequent Contributor
Posts: 78

Re: merge variables from save file to new variable and include in same file

Thanks for the quickk reply. Using the following code it does not seem to merge. 

 

data have;
set have;
merge var1 var2 var3;
run;

 

How does the merge happen and where does it go? This should be a trivial operation. I need to merge/combine multiple variables of the same format and equal length for vbar plotting. 

 

Also, I'm not clear on why the CAT's need to be used..

 

Thanks again.

Super User
Posts: 23,928

Re: merge variables from save file to new variable and include in same file

It is trivial, I don't think you understand the MERGE statement. It's intended to merge DATA SETS not variables. 

 

Post sample data if you need further assistance. CAT will combine variables into a single variable, which is what you seem to want. But it may not be the case, which is why you need to post data if you need further help.

 

new_variable= catt(var1, var2, var3);
Frequent Contributor
Posts: 78

Re: merge variables from save file to new variable and include in same file

Also, is merge the proper approach to this problem?

Frequent Contributor
Posts: 78

Re: merge variables from save file to new variable and include in same file

The input columns look like the below:

 

3  
 4 
  5
   
6  
   
 7 
   
  10
   
   

 

The desired output looks like

 

3
4
5
 
6
 
7
 
10
 
 

 

I hope this is clearer.

 

Best regards.

Solution
‎10-23-2017 03:57 PM
Super User
Posts: 23,928

Re: merge variables from save file to new variable and include in same file

New_Var = CoalesceC(var1, var2, var3);
☑ This topic is solved.

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

Discussion stats
  • 7 replies
  • 176 views
  • 0 likes
  • 2 in conversation