Help using Base SAS procedures

how can i compute new variable?

Accepted Solution Solved
Reply
Contributor
Posts: 21
Accepted Solution

how can i compute new variable?

/* This is the STATS301 start up file */
*Set up title on line 1;
TITLE1 "STATS 301 / 749750841 / glee217 : &sysuserid";
*Specify SAS library for storing permanent SAS datasets;
LIBNAME stats301 "C:\301";

***HERE
***import .sas7bdat file;
data temp;
set stats301.account_level_ds;
run;


data nonnegative;
set temp;
keep current_balance lending_interest;
if (current_balance <0) then delete;
rename lending_interest=yearint;
yearint = (current_balance)*0.03;
run;

title1 "nonnegative";
proc print data=nonnegative (obs=10);
run;

Hi I'm trying to create new variable called 'yearint' for data 'nonnegative' but somehow my code below doesn't work and it just gives me origianal 'lending_interest' value along to where it is not 'current_balance<0'

yearint = (current_balance)*0.03;

 how can i write down new variable and compute some new integer which is calculated with another variable?

 

Thank you


Accepted Solutions
Solution
‎09-16-2017 08:09 PM
Super User
Posts: 23,778

Re: how can i compute new variable?

You should ELSE IF instead of multiple IF and comment your code. It also seems weird to round one value and not the other for the same variable?

 

if (current_balance <0) then yearint = lending_interest;
else if (current_balance >=0) then yearint = int((current_balance)*0.03);

 


glee217 wrote:
data integer;
set temp;

if (current_balance <0) then yearint = lending_interest;

if (current_balance >=0) then yearint = int((current_balance)*0.03);
run;

proc print data=integer (obs=10);
var current_balance yearint run;

hi sorry but can i ask you a difference question? i still want to create variable 'yearint' but this time i want to use 'current_balance' variable directly.  And it didn't give me decimal places. do you know how to fix this?

 캡처.JPG




View solution in original post


All Replies
Super User
Super User
Posts: 9,606

Re: how can i compute new variable?

Your issue is likely the placement and use of rename - this is renaming another variable to be current balance.  This code below, renames the incoming variable, then your datasetp code will overwrite this.

Note some formatting changes:

title1 "STATS 301 / 749750841 / glee217 : &sysuserid";

libname stats301 "C:\301";

data nonnegative (keep=current_balance lending_interest);
  set stats301.account_level_ds (rename lending_interest=yearint);
  where current_balance < 0;
  yearint=(current_balance)*0.03;
run;

title1 "nonnegative";
proc print data=nonnegative (obs=10);
run;
Contributor
Posts: 21

Re: how can i compute new variable?

data integer;
set temp;

if (current_balance <0) then yearint = lending_interest;

if (current_balance >=0) then yearint = int((current_balance)*0.03);
run;

proc print data=integer (obs=10);
var current_balance yearint run;

hi sorry but can i ask you a difference question? i still want to create variable 'yearint' but this time i want to use 'current_balance' variable directly.  And it didn't give me decimal places. do you know how to fix this?

 캡처.JPG

Super User
Super User
Posts: 9,606

Re: how can i compute new variable?

You specify you want an integer result:

if (current_balance >=0) then yearint = int((current_balance)*0.03);

The int() is a function which returns only a hole number, remove that if you want decimals. 

Solution
‎09-16-2017 08:09 PM
Super User
Posts: 23,778

Re: how can i compute new variable?

You should ELSE IF instead of multiple IF and comment your code. It also seems weird to round one value and not the other for the same variable?

 

if (current_balance <0) then yearint = lending_interest;
else if (current_balance >=0) then yearint = int((current_balance)*0.03);

 


glee217 wrote:
data integer;
set temp;

if (current_balance <0) then yearint = lending_interest;

if (current_balance >=0) then yearint = int((current_balance)*0.03);
run;

proc print data=integer (obs=10);
var current_balance yearint run;

hi sorry but can i ask you a difference question? i still want to create variable 'yearint' but this time i want to use 'current_balance' variable directly.  And it didn't give me decimal places. do you know how to fix this?

 캡처.JPG




☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 370 views
  • 0 likes
  • 3 in conversation