Help using Base SAS procedures

How do I do an Array or Macro?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 7
Accepted Solution

How do I do an Array or Macro?

Hello everyone,

 

I need to mask rows in a set of columns. For example, say I have five columns named Column 1 - 5. There are thousands of rows in each column and anything under a certain number I need to mask. I could do this in a datastep repeating the code for each Column name but the problem is that I have a large number of columns in my actual dataset. Is there a way to do an Array or use a Macro to mask the data?

 

Thanks!

 

DATA NEW_DATA; SET OLD_DATA;

 

IF COLUMN_1 < 10 THEN COLUMN_1 = .;

ETC...


Accepted Solutions
Solution
‎09-07-2016 11:59 AM
Super User
Posts: 10,466

Re: How do I do an Array or Macro?

Your requirement for "NC" and "<10" not possible if the variables are numeric. BUT a custom format will work for most purposes:

 

Here is an example format

proc format library=work;
   Value MyValues
   . = 'NC'
   low - 10 = "<10"
   other = [Best10.]
   ;
run;


To see the effect

proc print data=have ;
   var <your variable names go here>;
   Format <list the variables that need the display format> MyValues. ;
run;

replace the text in <> above with your variable names and no <>.  Note the period at the end of MyValues in the Format statement. The period tells SAS that MyValues is the name of the format to apply to the variables listed before it.

 

View solution in original post


All Replies
Super User
Posts: 10,466

Re: How do I do an Array or Macro?

I would ask what is the purpose of this "mask". If it is for printing to suppress values then it may be easier to use a format to display a blank;

 

If your condition for "masking" is the same for each variable and you actually want to remove the value from the data set then an array would be a good choice such as this:

 

data want;
   set have;
   array a <list names of variables>;
   do i = 1 to dim(a);
      if a[i] < 10 then a[i] = .;
   end;
run;

Which would set all the variables to missing if their value is less than 10.

 

If each variable has a different condtion then neither a macro or array will work.

Occasional Contributor
Posts: 7

Re: How do I do an Array or Macro?

Thank you for the quick reply.

 

All the variables have the same conditions but if the variable is blank then it needs to read NC and if is is < 10 then it needs to read < 10.

 

Thanks again!

Solution
‎09-07-2016 11:59 AM
Super User
Posts: 10,466

Re: How do I do an Array or Macro?

Your requirement for "NC" and "<10" not possible if the variables are numeric. BUT a custom format will work for most purposes:

 

Here is an example format

proc format library=work;
   Value MyValues
   . = 'NC'
   low - 10 = "<10"
   other = [Best10.]
   ;
run;


To see the effect

proc print data=have ;
   var <your variable names go here>;
   Format <list the variables that need the display format> MyValues. ;
run;

replace the text in <> above with your variable names and no <>.  Note the period at the end of MyValues in the Format statement. The period tells SAS that MyValues is the name of the format to apply to the variables listed before it.

 

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 315 views
  • 0 likes
  • 2 in conversation