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

I know this is probably bread and butter stuff in macro.  I am trying to delete missing observations for ch_aic and ch_gluc in my dataset by calling a macro. I normally do this in an if-then delete statement (as below) but I want to implement it in a macro. Any help will be appreciated. I used 

data us; set me; 

%let Ch_aic = if &dep = . then delete;

%let Ch_gluc = if &dep = . then delete;
run;
This didn't run. What could be wrong with this code?

  data me;
  set you;
  Ch_aic = Postaic-preaic;
  Ch_gluc= Postgluc-Pregluc;
  run;

  data us;
  set me;
  if Ch_gluc = . then delete
  if  Ch_aic =. then delete;
run;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

What went wrong? you never used the macro variables. And the macro variable DEP doesn't appear to have a value assigned either.  Did you read the log? I might expect a message

WARNING: Apparent symbolic reference DEP not resolved.

When you see that message it means you attempted to use a variable when it does not have a value assigned.

 

Something like this maybe?

  data junk;
  input x y $;
  datalines;
  1  abc
  2  cdf
  .  pdq
  4  .
  ;

  %macro mydelete(var=);
  if missing(&var.) then delete;
  %mend;

  data junk2;
    set junk;
    %mydelete(var=x);
    %mydelete(var=y);
  run;

Doesn't save much code from an if/then.

Note that I use the Missing function because it will detect missing character as well as numeric variables.

 

If your real question is to delete a record if any of a bunch of numeric variables is missing then a macro isn't needed.

  data junk;
  input x1-x4  y $;
  datalines;
  1 11 111 1111 abc
  2 22 222 2222 cdf
  . 33 333 3333 pdq
  4  . 444 4444 lkj
  5  . .   .    kkk
  ;

 data junk2;
    set junk;
    if nmiss(of x1-x4) > 0 then delete;
 run;

NMISS counts the number of numeric values that are missing in a list. So if any of the X variables in the above example are missing then the result from the function is greater than 0 and the record is deleted. The "of x1-x4" is one of the short hand ways to create lists of variables so they don't all have to be typed.

 

Character variables take more work depending on the possible content.

View solution in original post

4 REPLIES 4
ballardw
Super User

What went wrong? you never used the macro variables. And the macro variable DEP doesn't appear to have a value assigned either.  Did you read the log? I might expect a message

WARNING: Apparent symbolic reference DEP not resolved.

When you see that message it means you attempted to use a variable when it does not have a value assigned.

 

Something like this maybe?

  data junk;
  input x y $;
  datalines;
  1  abc
  2  cdf
  .  pdq
  4  .
  ;

  %macro mydelete(var=);
  if missing(&var.) then delete;
  %mend;

  data junk2;
    set junk;
    %mydelete(var=x);
    %mydelete(var=y);
  run;

Doesn't save much code from an if/then.

Note that I use the Missing function because it will detect missing character as well as numeric variables.

 

If your real question is to delete a record if any of a bunch of numeric variables is missing then a macro isn't needed.

  data junk;
  input x1-x4  y $;
  datalines;
  1 11 111 1111 abc
  2 22 222 2222 cdf
  . 33 333 3333 pdq
  4  . 444 4444 lkj
  5  . .   .    kkk
  ;

 data junk2;
    set junk;
    if nmiss(of x1-x4) > 0 then delete;
 run;

NMISS counts the number of numeric values that are missing in a list. So if any of the X variables in the above example are missing then the result from the function is greater than 0 and the record is deleted. The "of x1-x4" is one of the short hand ways to create lists of variables so they don't all have to be typed.

 

Character variables take more work depending on the possible content.

ChuksManuel
Pyrite | Level 9

Thanks. That's exactly what i wanted.

 

andreas_lds
Jade | Level 19

Please discard the idea of using macro-code as it seems pointless. Also note, that comparing a variable with . will only work without funny notes in the log, if the variable is numeric. If you want don't want to think about the type of a variable, use

if missing(variable) then delete;
ChuksManuel
Pyrite | Level 9

I am learning macro hence the question. Thanks for the input.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1127 views
  • 0 likes
  • 3 in conversation