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;
Text has to match exactly. You're missing a $ sign.
"$100k to $149k"
Thank you for the fix! I guess after looking it over so many times, I ended up overlooking the obvious.
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).
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.
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Select SAS Training centers are offering in-person courses. View upcoming courses for: