DATA Step, Macro, Functions and more

creating new variables in if-then statements

Reply
Contributor
Posts: 22

creating new variables in if-then statements

 

data employees;
infile 'c:\users\************\*******\employees.dat';
input ssn :$1-11 name & $12-28 dob :date10. paygrade $ monsal :comma6.2 job & :$71-100;
if paygrade = 'gr20' then do;
minimum = 50000.00;
maximum = 70000.00;
end;

age = int(yrdif(dob, today (), 'age'));
put monsal dollar7.2;

run;

I am trying to create min and max variables based on the paygrade codes. This is an exercise that comes out of the "The Little SAS Book." When I run this program I get two new variables for minimum and maximum but there are just periods in the columns. I tried using 50000 instead of 50000.00 and that didn't work either. Do I have a formatting problem with the numeric data or is my program not written correctly.

 

This is exercise 31d from Chapter 3 in "Exercises and Projects for The Little SAS Book."

 

Trusted Advisor
Posts: 1,932

Re: creating new variables in if-then statements

[ Edited ]
Posted in reply to andrewfau

You get the periods because at no time in your data do you have paygrade='gr20'. I don't know if this is just a capitalization mismatch, or that 'gr20' never appears in your data. You need to write code to tell SAS what it should do if it finds paygrade is not equal to 'gr20'.

Contributor
Posts: 22

Re: creating new variables in if-then statements

Posted in reply to PaigeMiller

Untitled.jpg

Contributor
Posts: 22

Re: creating new variables in if-then statements

Posted in reply to PaigeMiller
data employees;

input ssn :$1-11 name & $12-28 dob :date10. paygrade $ monsal :comma6.2 job & :$71-100;
if paygrade = 'GR20' then do;
minimum = 50000;
maximum = 70000;
end;
if paygrade = 'GR21' then do;
minimum = 55000;
maximum = 75000;
end;
if paygrade = 'GR22' then do;
minimum = 60000;
maximum = 85000;
end;
if paygrade = 'GR23' then do;
minimum = 70000;
maximum = 100000;
end;
if paygrade = 'GR24' then do;
minimum = 80000;
maximum = 120000;
end;
if paygrade = 'GR25' then do;
minimum = 100000;
maximum = 150000;
end;
if paygrade = 'GR26' then do;
minimum = 120000;
maximum = 200000;
end;
age = int(yrdif(dob, today (), 'age'));
put monsal dollar7.2;

run;

Is there a way i can condense those if then statements or do I need to write them all individually for a problem like this?

Super User
Posts: 19,861

Re: creating new variables in if-then statements

Posted in reply to andrewfau

You could probably do an array. Not sure it's less code in the end, but easier to manage perhaps?

 

array min_pay(20:26) (50000 55000 60000 70000 ... 120000);
array max_pay(20:26) (70000 75000 .. 200000);

grade = input(compress(paygrade, ,'kd'), 8.);
minimum = min_pay(grade);
maximum = max_pay(grade);
Frequent Contributor
Posts: 149

Re: creating new variables in if-then statements

Posted in reply to andrewfau
Things total check in the data-file: is there any obs having paygrade "gr20"? Both letters have time be in low case.
Maximum and minimum are only set of paygrade is gr20, any obs having another value will show missing value fit both vars.
Contributor
Posts: 22

Re: creating new variables in if-then statements

Posted in reply to error_prone
Gotcha. It was the lowercase that was the problem. Thanks!
Ask a Question
Discussion stats
  • 6 replies
  • 140 views
  • 0 likes
  • 4 in conversation