DATA Step, Macro, Functions and more

New variable assignment not working?

Accepted Solution Solved
Reply
Contributor
Posts: 61
Accepted Solution

New variable assignment not working?

Hi, All,

I am a little confused that variable age2 is not showing up.

It will work if      'age2=age*1;'     was put behind     'set have;'   .

Can somebody tell me why?

Thanks.

Joe

data have;

input sex$ age;

datalines;

F 14

M 15

;

data male female;

set have;

if sex='M' then output male;

else if sex='F' then output female;

age2=age*1;

run;


Accepted Solutions
Solution
‎06-15-2015 12:02 PM
Super User
Posts: 17,829

Re: New variable assignment not working?

Order of operations. Your output statement is before the calculation, so when SAS writes out the observation it hasn't been calculated yet.  

You do get the age2 variable but it is all missing.

View solution in original post


All Replies
Solution
‎06-15-2015 12:02 PM
Super User
Posts: 17,829

Re: New variable assignment not working?

Order of operations. Your output statement is before the calculation, so when SAS writes out the observation it hasn't been calculated yet.  

You do get the age2 variable but it is all missing.

Contributor
Posts: 61

Re: New variable assignment not working?

Thanks.

Joe

Contributor
Posts: 30

Re: New variable assignment not working?

Hi ,

You can use the below set of code ,by moving the age2 before the output statements.

data have;

input sex$ age;

datalines;

F 14

M 15

;

data male female;

set have;

age2=age*1;

if sex='M' then output male;

else if sex='F' then output female;

run;

☑ This topic is SOLVED.

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

Discussion stats
  • 3 replies
  • 232 views
  • 0 likes
  • 3 in conversation