DATA Step, Macro, Functions and more

Adding new Variables

Reply
Frequent Contributor
Posts: 116

Adding new Variables

Hi SAS experts,

 

I want to add new variables into the dataset.

 

data new;
input name$ marks year;
datalines;
abc 10 2015
ab 20 2016
ba 30 2017
ca 10 2015
bv 20 2016
ka 20 2017
;
run;

 

i want to  add 3 variables in new data set
1.marks_1 = marks * 150
2.marks_2 = marks * 250
3.marks_3 = marks * 350

 

for example:
name _2017_MARKS MARKS_1 MARKS_2 MARKS_3
ba   30         4500     7500    10500
ka   20         3000     5000      7000

 

Similarly for 2015,2016 also. please guide.

 

Thanks & regards,

Sanjay

PROC Star
Posts: 554

Re: Adding new Variables

like this?

 

data new;
input name$ marks year;
datalines;
abc 10 2015
ab 20 2016
ba 30 2017
ca 10 2015
bv 20 2016
ka 20 2017
;

data new;
   set new;
   marks_1 = marks * 150;
   marks_2 = marks * 250;
   marks_3 = marks * 350;
run;
Super User
Posts: 6,971

Re: Adding new Variables

What is the purpose of renaming marks to _2017_marks?

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Super User
Posts: 7,430

Re: Adding new Variables

Tip, don't put "data" into column names or dataset names, use simple naming conventions like <prefix><incrementor> so that you can easily refer to variables and groups of variables using inbuilt SAS commands like:

VAR1 VAR2 VAR3, can be VAR:, or VAR1--VAR3, or array VAR{3}

Putting data in coumns names/dataset names really just overcomplicates your coding time and effort, and makes unmanageable code.  Note, this doesn't apply to Labels, put whatever you like in them.

Ask a Question
Discussion stats
  • 3 replies
  • 167 views
  • 0 likes
  • 4 in conversation