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

Hi All,

I feel like I am missing a basic  concept -- I need a macro variable to resolve to missing if the required value is not populated. This is part of  a loop. The code loops through each patient (&p_id). eg:  If expdose or exdstxt is not valued, then &headerdose should resolve to missing.

 

data set_header2;

set ex;

where subject = "&p_id";

if not missing(expdose) then call symput("headerdose",trim(expdose));

else if not missing(exdstxt) then call symput("headerdose",trim(exdstxt));

else call symput("headerdose", NA);  ===> does not work.

 

%put &headerdose;

getting message WARNING: Apparent symbolic reference HEADERDOSE not resolved.

thank you.

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Because your current code does not have a RUN statement to end the data step the %PUT is trying to reference the macro variable while the data step is being compiled, before it has had a chance to run.  Also if no observations match your WHERE clause then the data step will stop when it runs the SET statement. 

 

Set the default value before the data step.

%let headerdose=No such subject;
data set_header2;
  set ex;
  where subject = "&p_id";
  if not missing(expdose) then call symputx("headerdose",expdose);
  else if not missing(exdstxt) then call symputx("headerdose",exdstxt);
  else call symput("headerdose", trimn(' '));  
run;
%put Headerdose = |&headerdose|;

View solution in original post

5 REPLIES 5
Reeza
Super User
Unless NA (R programming?) is a variable that has a missing variable that isn't how you specify missing in SAS.

else call symput("headerdose", "");

I would also strongly recommend you use call symputX() which has some extra features that help, like automatically trimming trailing spaces and allows you to specify if the macro variable is local or global.
NJGIRL
Obsidian | Level 7

Hi Reeza,

 

Unfortunately, I am still getting the same error if expdose or exdstxt are missing (btw, they are character variables)

data set_header2;

set ex;

where subject = "&p_id";

if not missing(expdose) then call symput("headerdose",trim(expdose));

else if not missing(exdstxt) then call symput("headerdose",trim(exdstxt));

else call symput("headerdose", "" );

run;

 

LOG:

WARNING: Apparent symbolic reference HEADERDOSE not resolved.

let me know if you have any other ideas. thanks! 🙂

 

Tom
Super User Tom
Super User

Because your current code does not have a RUN statement to end the data step the %PUT is trying to reference the macro variable while the data step is being compiled, before it has had a chance to run.  Also if no observations match your WHERE clause then the data step will stop when it runs the SET statement. 

 

Set the default value before the data step.

%let headerdose=No such subject;
data set_header2;
  set ex;
  where subject = "&p_id";
  if not missing(expdose) then call symputx("headerdose",expdose);
  else if not missing(exdstxt) then call symputx("headerdose",exdstxt);
  else call symput("headerdose", trimn(' '));  
run;
%put Headerdose = |&headerdose|;
NJGIRL
Obsidian | Level 7

Tom, I am no longer getting an error in the log.

your code works:

 

%let headerdose = --not avail;

 

data set_header2;

set ex;

* where subject = "&p_id";

if subject = "&p_id";

if not missing(expdose) then call symput("headerdose",trim(expdose));

else if not missing(exdstxt) then call symput("headerdose",trim(exdstxt));

else call symput("headerdose", trimn(' ' ));

run;

%put &headerdose; run;

 

Good point regarding the 'where' clause... I replaced it with an 'if' statement.

Thanks. this save me significant research time (which do not have today:)

 

 

 

novinosrin
Tourmaline | Level 20

HI @NJGIRL  It appears the question has been answered. Requesting you to please mark your chosen answer and close the thread. Thank you in advance. Have a nice day!

SAS Innovate 2025: Call for Content

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 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 5 replies
  • 1025 views
  • 1 like
  • 4 in conversation