BookmarkSubscribeRSS Feed
tianerhu
Pyrite | Level 9

when I just have one SAS data set , I know how to set up a new MM variable, like below:

data A;

set C;

input score1 score2;

MM=score1+score2;

run;

if have two data sets ,dataA and dataB, I just want to set up a new MM variable for data set A, What should I do?

 

5 REPLIES 5
Kurt_Bremser
Super User

You quite obviously do NOT know how to calculate the variable from one dataset:

 79         data A;
 80         set C;
 81         input score1 score2;
 82         MM=score1+score2;
 83         run;
 
 ERROR: No DATALINES or INFILE statement.
 NOTE: The SAS System stopped processing this step because of errors.

or be more diligent when posting your code here. By posting garbage, you make it very hard for us to know what you want to do.

 


@tianerhu wrote:

when I just have one SAS data set , I know how to set up a new MM variable, like below:

data A;

set C;

input score1 score2;

MM=score1+score2;

run;

if have two data sets ,dataA and dataB, I just want to set up a new MM variable for data set A, What should I do?

 


 

tianerhu
Pyrite | Level 9

There is something wrong with the code which I posted. Sorry about that.

Thank you for your reply.

Tom
Super User Tom
Super User

Remove the  INPUT statement. That is for reading text from a file.  You appear to already have a SAS dataset with the variables SCORE1 and SCORE2 on it.

data a;
  set c;
  mm= score1 + score2;
run;

Note that variables do not live independent of datasets.  So to add a new variable the A you need to either recreate A or create a new dataset that has the data from A and calculates the new variable(s).  So the example above creates a new dataset named A by using the data already in C.  While it is copying the data it also creates MM.

If you are just a beginner do not try to overwrite your dataset by using the same name as the input and the output dataset.  That will remove the original version of the dataset and make it hard to fix and coding mistakes.

tianerhu
Pyrite | Level 9

Thank you for your help.

Reeza
Super User

@tianerhu wrote:

 

if have two data sets ,dataA and dataB, I just want to set up a new MM variable for data set A, What should I do?

 


Why do you think the second data set comes in to play at all? Why can't you just ignore it?

Data sets are independent unless you need information from both. But if that's the case you need to generally create a new output data set. 

 

It's better to not think of it as adding to a data set but as creating a new data set with a new variable added on. DBs will overwrite a data set, but in SAS it recreates it entirely, it's not a modification/update. This means it drops the original table, creates a new one and the old one is deleted. This happens in the background but it happens. This is different from R/Python logic. 

 

So if your process is as follows:

  1. Create a new variable MM as the sum of var1, var2 from data set A
  2. Add a new variable CC from data set B via a merge on ID

You should end up with 

 

Data set A - original

Data set B - original

Data set A2 - from Step 1

Data set A3 - from Step 3. 

 

A2 can be dropped later if needed. If you can't understand the data flow process you can try drawing it out on paper and it will help and save you a lot of time. 

 

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
  • 5 replies
  • 648 views
  • 0 likes
  • 4 in conversation