Hi all, I'm trying to extract values from a surveyfreq procedure using two types of code structure and they both give out 0 observations. Below are the two statement types.
First
DATA &YES &NO &ALLTOTAL;
SET COMBINEALLtesTaBC;
SELECT (F_&DISEASE);
WHEN ('&YES') OUTPUT &YES;
WHEN ('&NO') OUTPUT &NO;
WHEN ('TOTAL') OUTPUT &ALLTOTAL;
OTHERWISE;
END;
RUN;
Second
DATA &YES &NO &ALLTOTAL;
SET COMBINEALLtesTABC;
if F_&disease = "YES" THEN OUTPUT &YES;
if F_&disease = "NO" THEN OUTPUT &NO;
if F_&disease = "TOTAL" THEN OUTPUT &ALLTOTAL;
RUN;
What am I doing wrong? I included the dataset and the log when i run the code.
Thank you all.
There are two issues in your code
1) I agree with @smantha , one reason is you need to use " " to resolve the macro variable and used to subset
2) Also the important thing i noticed is that though your second code is correct, it did not work and the reason is because that the macro variables resolved to YES, NO and TOTAL all of which are upcase whereas in F_HADSTRK variable actually has propcase values so the ideal way to make your second code work is as below
DATA &YES &NO &ALLTOTAL;
SET COMBINEALLtesTABC;
if F_&disease = "Yes" THEN OUTPUT &YES;
if F_&disease = "No" THEN OUTPUT &NO;
if F_&disease = "Total" THEN OUTPUT &ALLTOTAL;
RUN;
Also to make your first code work try below
DATA &YES &NO &ALLTOTAL;
SET COMBINEALLtesTaBC;
SELECT (F_&DISEASE);
WHEN ("Yes") OUTPUT &YES;
WHEN ("No") OUTPUT &NO;
WHEN ("TOTAL'") OUTPUT &ALLTOTAL;
OTHERWISE;
END;
RUN;
What @smantha says.
On top of it: I'd always also add an output table for "otherwise" so you collect what doesn't get selected by any of the other conditions.
There are two issues in your code
1) I agree with @smantha , one reason is you need to use " " to resolve the macro variable and used to subset
2) Also the important thing i noticed is that though your second code is correct, it did not work and the reason is because that the macro variables resolved to YES, NO and TOTAL all of which are upcase whereas in F_HADSTRK variable actually has propcase values so the ideal way to make your second code work is as below
DATA &YES &NO &ALLTOTAL;
SET COMBINEALLtesTABC;
if F_&disease = "Yes" THEN OUTPUT &YES;
if F_&disease = "No" THEN OUTPUT &NO;
if F_&disease = "Total" THEN OUTPUT &ALLTOTAL;
RUN;
Also to make your first code work try below
DATA &YES &NO &ALLTOTAL;
SET COMBINEALLtesTaBC;
SELECT (F_&DISEASE);
WHEN ("Yes") OUTPUT &YES;
WHEN ("No") OUTPUT &NO;
WHEN ("TOTAL'") OUTPUT &ALLTOTAL;
OTHERWISE;
END;
RUN;
Thank you. I designated the macro variables as upcase rather than the proper case, that's on me. However, I'd like to ask, of the two codes, which do you consider the most effective? Why is that? I am still learning the coding language and would rather distill my codes to efficient and simple statements rather than convoluted ones.
Cheers.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.