BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
kmh
Calcite | Level 5 kmh
Calcite | Level 5

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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.

View solution in original post

9 REPLIES 9
novinosrin
Tourmaline | Level 20

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;

 

 

kmh
Calcite | Level 5 kmh
Calcite | Level 5
thank you very much!
Astounding
PROC Star

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.

kmh
Calcite | Level 5 kmh
Calcite | Level 5

Thank you! I'll keep that in mind.  I'm learning! 🙂

Tom
Super User Tom
Super User

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.

kmh
Calcite | Level 5 kmh
Calcite | Level 5

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? 

Tom
Super User Tom
Super User

@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;
Kurt_Bremser
Super User

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.

kmh
Calcite | Level 5 kmh
Calcite | Level 5

Thank you! This worked perfectly!

Ready to join fellow brilliant minds for the SAS Hackathon?

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!
SAS Enterprise Guide vs. SAS Studio

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 9 replies
  • 2000 views
  • 0 likes
  • 5 in conversation