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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Jagadishkatam
Amethyst | Level 16

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;
Thanks,
Jag

View solution in original post

4 REPLIES 4
smantha
Lapis Lazuli | Level 10
In case of when the macro vars are in single quotes. So they are not resolved. Use double qoutes instead.
In the case of if did you try checking the case of values and if the values have padded blanks?
Patrick
Opal | Level 21

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.

Jagadishkatam
Amethyst | Level 16

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;
Thanks,
Jag
sas_apprenant
Fluorite | Level 6

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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

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.

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
  • 4 replies
  • 589 views
  • 4 likes
  • 4 in conversation