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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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