DATA Step, Macro, Functions and more

How to add a new column in existing dataset

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 12
Accepted Solution

How to add a new column in existing dataset

Hello,

 

I want to add new columns to an existing dataset. The new column should contain the data from another dataset. 

 

Here it looks like this

 

 

data sumOut;
set work.dresssales1;
sumvar=sum(_29_8_2013,_31_8_2013);
run;

 

I want to add the value that is stored in sumvar to a new dataset Retail. How do I do this ?

 

Regards,

 

Aditya

 

 

 


Accepted Solutions
Solution
‎08-30-2017 12:57 PM
SAS Employee
Posts: 4

Re: How to add a new column in existing dataset

Posted in reply to AdityaKir

Without the information @ballardw asked for it is difficult to answer your question @AdityaKir.  The code you provided already creates and adds a new column.  However, it appears that you want to take the new variable sumvar, which is in the data set SumOut, and add it to a data set called Retail.  

 

If SumOut contains only one record this code will add the sumvar value to every observation in Retail:

 

data Retail
set Retail;
if _n_=1 then set SumOut(keep=sumvar);
run;

 

If SumOut contains multiple records and an id variable this code will merge the sumvar value to the matching id value in Retail:

 

data Retail;
merge Retail SumOut(keep=id sumvar);
by id;
run;

 

View solution in original post


All Replies
Super User
Posts: 11,343

Re: How to add a new column in existing dataset

Posted in reply to AdityaKir

Do you have variables in both data sets to identify which records are supposed to align such as a product identifier, store identifier, dates or possibly multiple variables? If you have those key variables are combinations of them duplicated within one or both of the data sets?

 

It would be better to provide examples of BOTH data sets. This link https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... has instructions on how to make data step code to replicate the data that you can post here or attach as a TEXT file.

It is also a very good idea to provide an example of what your are looking for as a result in case your narrative description is incomplete.

Solution
‎08-30-2017 12:57 PM
SAS Employee
Posts: 4

Re: How to add a new column in existing dataset

Posted in reply to AdityaKir

Without the information @ballardw asked for it is difficult to answer your question @AdityaKir.  The code you provided already creates and adds a new column.  However, it appears that you want to take the new variable sumvar, which is in the data set SumOut, and add it to a data set called Retail.  

 

If SumOut contains only one record this code will add the sumvar value to every observation in Retail:

 

data Retail
set Retail;
if _n_=1 then set SumOut(keep=sumvar);
run;

 

If SumOut contains multiple records and an id variable this code will merge the sumvar value to the matching id value in Retail:

 

data Retail;
merge Retail SumOut(keep=id sumvar);
by id;
run;

 

☑ This topic is solved.

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

Discussion stats
  • 2 replies
  • 4830 views
  • 1 like
  • 3 in conversation