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

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

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
JaneEslinger
SAS Employee

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

2 REPLIES 2
ballardw
Super User

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.

JaneEslinger
SAS Employee

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;

 

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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
  • 2 replies
  • 48287 views
  • 1 like
  • 3 in conversation