Hello. I'm having some basic problems. First, when trying to create a new variable with if then statements I kept getting the error code 180 "Statement is not valid or it is used out of proper order". I finally figured out that when I used the code below I was able to get the if then statements to work. But I'm finding that I have to include the data step with every variable I want to create. Now, when I try to create a table with two variables I get an error that one of the variables cannot be found. I'm not really sure why the if/then statements wouldn't work at first and why I have to now include the data step every time. Any help would be appreciated for this frustrated novice 🙂 Thank you!
data a;
set edat.els0212byf3pststu_;
latino=.;
if bys15=1 then latino=1; *hispanic;
if bys15=0 then latino=0; *non-hispanic;
if bys15 in (-1,-2,-4,-6,-8,-9) then latino=.;
else if latino=. then delete;
run;
proc freq data=a;
tables latino;
run;
data a;
set edat.els0212byf3pststu_;
male=.;
if bys14=1 then male=1; *male;
if bys14=2 then male=0; *female;
if bys14 in (-2,-4,-8,-9) then male=.;
else if male=. then delete;
run;
proc surveyfreq data=a;
tables latino*male;
run;
I
Don't think so complicated. Steps can be combined, so this should do it:
data a;
set edat.els0212byf3pststu_;
select (bys15);
when (1) latino = 1; *hispanic;
when (0) latino = 0; *non-hispanic;
otherwise delete;
end;
select (bys14);
when (1) male = 1; *male;
when (2) male = 0; *female;
otherwise delete;
end;
run;
proc freq data=a;
tables latino;
tables latino*male;
run;
Note the use of select() (the SAS version of a case statement) instead of the if-then-else chains.
first off, since your conditions are mutually exclusive, I would use else if and not if in repetition to check every if.
data a;
set edat.els0212byf3pststu_;
latino=.;/*not needed, coz new variable is anyway initialized to missing*/
if bys15=1 then latino=1; *hispanic;
else if bys15=0 then latino=0; *non-hispanic;
else if bys15 in (-1,-2,-4,-6,-8,-9) then latino=.;/*else*/if latino=. then delete;
run;
data a;
set edat.els0212byf3pststu_;
male=.;/*not needed, coz new variable is anyway initialized to missing*/
if bys14=1 then male=1; *male;
else if bys14=2 then male=0; *female;
else if bys14 in (-2,-4,-8,-9) then male=.;
/*else*/if male=. then delete;
run;
Combine into one step:
data a;
set edat.els0212byf3pststu_;
latino=.;/*not needed, coz new variable is anyway initialized to missing*/
if bys15=1 then latino=1; *hispanic;
else if bys15=0 then latino=0; *non-hispanic;
else if bys15 in (-1,-2,-4,-6,-8,-9) then latino=.;/*else*/if latino=. then delete;
if bys14=1 then male=1; *male;
else if bys14=2 then male=0; *female;
else if bys14 in (-2,-4,-8,-9) then male=.;
/*else*/if male=. then delete;
run;
You need to appreciate that your steps run in order. One DATA or PROC step runs, then the next one, then the next one, etc. That will explain part of the mystery. But you could certainly create both variables in a single step:
data a;
set edat.els0212byf3pststu_;
if bys15=1 then latino=1; *hispanic;
if bys15=0 then latino=0; *non-hispanic;
if bys14=1 then male=1; *male;
if bys14=2 then male=0; *female;
run;
proc freq data=a;
tables latino;
where latino > .;
run;
proc surveyfreq data=a;
tables latino*male;
where latino > . and male > .;
run;
You don't absolutely require the WHERE statements ... it's only a matter of whether you want missing values to be counted by PROC FREQ. Try it both ways (with and without WHERE statements) and see what you prefer.
Thank you! I'll keep that in mind. I'm learning! 🙂
Why would you expect that the second version of dataset A would also include the variables from the version of A that you deleted by making another dataset with the same name?
Either create both variables in the same data step. Or tell the second data step to use the output of the first data step as the input instead of going back to the source data.
Thank you. You say "tell the second data step to use the output of the first data step as the input instead of going back to the source data.". How exactly would I do that?
@kmh wrote:
Thank you. You say "tell the second data step to use the output of the first data step as the input instead of going back to the source data.". How exactly would I do that?
This step will create a dataset called STEP1 from a dataset call OLD.
data step1;
set old;
run;
If you then want to use that data and make more calculations then use as the data to be read. So you could create STEP2 from STEP1 with a data step like this.
data step2;
set step1;
run;
Don't think so complicated. Steps can be combined, so this should do it:
data a;
set edat.els0212byf3pststu_;
select (bys15);
when (1) latino = 1; *hispanic;
when (0) latino = 0; *non-hispanic;
otherwise delete;
end;
select (bys14);
when (1) male = 1; *male;
when (2) male = 0; *female;
otherwise delete;
end;
run;
proc freq data=a;
tables latino;
tables latino*male;
run;
Note the use of select() (the SAS version of a case statement) instead of the if-then-else chains.
Thank you! This worked perfectly!
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.
Select SAS Training centers are offering in-person courses. View upcoming courses for: