How do I correctly use an IF statement to assign new data to a variable?

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

How do I correctly use an IF statement to assign new data to a variable?

I wrote some code that is only partially executing (below) and I cannot determine why it is not completely working.  I am not receiving an error message.  Part of the code is simply not filling in the character data that I've specified.  Sample data is attached.  A little urgent help please:

 

data &OUTFILE3.;
set &OUTFILE3.;

LENGTH IMP_JOB2 $30;

if IMP_JOB = "z_Blue Collar"
and INC_CAT in ("$150k to 199k","$200k to 249k","$250k or More")
then IMP_JOB2 = "Doctor";                 /* Correctly executed */
else if IMP_JOB = "z_Blue Collar"
and INC_CAT in ("$100k to 149k")      /* Either not executing or being overwritten somehow */
then IMP_JOB2 = "z_White Collar";
else if IMP_JOB = "z_Blue Collar"
and INC_CAT = "None"
then IMP_JOB2 = "Unemployed";       /* 0 records, so uncertain if it executed properly */
else IMP_JOB2 = IMP_JOB;
run;


Accepted Solutions
Solution
‎09-26-2016 07:55 PM
Super User
Posts: 19,192

Re: How do I correctly use an IF statement to assign new data to a variable?

Text has to match exactly. You're missing a $ sign.

 

"$100k to $149k"

 

View solution in original post


All Replies
Solution
‎09-26-2016 07:55 PM
Super User
Posts: 19,192

Re: How do I correctly use an IF statement to assign new data to a variable?

Text has to match exactly. You're missing a $ sign.

 

"$100k to $149k"

 

New Contributor
Posts: 3

Re: How do I correctly use an IF statement to assign new data to a variable?

[ Edited ]

Thank you for the fix!  I guess after looking it over so many times, I ended up overlooking the obvious.

Super User
Super User
Posts: 7,727

Re: How do I correctly use an IF statement to assign new data to a variable?

Several tips to help.  First, don't post Excel files, they are dangerous, post test data in the form of a datastep in the post.  Second use the code window = {i} above the post, so code retains formatting.  Then apply some good coding practices to make code readable.  Then alter your logic slightly to make it more readbable.  Not sure why you have a macro variable as dataset either.

 

data &outfile3.;
  set &outfile3.;
  length imp_job2 $30;
  if imp_job="z_Blue Collar" then do;
    select(inc_cat);
      when ("$150k to 199k","$200k to 249k","$250k or More") imp_job2="Doctor";
      when ("$100k to 149k") imp_job2="z_White Collar";
      when ("None") imp_job2="Unemployed";
      otherwise imp_job2=imp_job;
    end;
  end;
run;

Note, I have not added @Reeza's fix, as can't see the test data.  I would advise that if you intend to use that data for anything, then process it and convert the text to numbers so you can process the data effectively (i.e. do sums and things).

 

 

 

 

 

New Contributor
Posts: 3

Re: How do I correctly use an IF statement to assign new data to a variable?

Thanks so much.  I wasn't familiar with using "when" and have, thus, never used it before.  However, I can see how it can be useful especially for formatting and readability purposes.  All great advice that I really appreciate as I continue to learn.

Super User
Posts: 11,144

Re: How do I correctly use an IF statement to assign new data to a variable?

BTW

I strongly recommend not using the

data &OUTFILE3.;
   set &OUTFILE3.;

 

with anything except temporary datasets. It is too easy to change a variable such as you INC_CAT in one run (possibly "cleaning" the data) and then no longer have the original vales when needed.

 

I have traced a "result" that changed from nearly 30% in one year to about 70% in the next back to this dataset reuse and a recode statement that was flipping values such as 0 to 1 and 1 to 0.

☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 608 views
  • 3 likes
  • 4 in conversation