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

 it can take parameters to accept the low-range and high-range and see if the value is within the range then display the exact range but if the value is less than or greater than the range then display '$$$'.

how do i make this code generic... can u please suggest a solution.


%macro check (dsn=, dsn2=);
data &dsn;
set &indsn (Keep=Age);
run;

proc format;
value band 10 - <12 = '11 - 12'
other = '$$$';
run;

proc print data= &dsn;
format age band.;
run;

%mend check;
%check (dsn=, dsn2=);

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

If you are taking parameters for the macro then why didn't you show them in your macro.

Also the way  you have constructed this format, even as an example
proc format;
value Numericband

10 - <12 = '11 - 12'
15 - <25 = '16 - 21'   (why do you confuse 25 and 21 here by the way)
other = '***';
run;

actually has 2 ranges. So if you give a single High and single low value how do you construct 2 ranges?

This is an example of creating a single range with a format

 

%macro numeric_band_check (indsn=, dsn=, low=, high=);
data &dsn;
set &indsn (Keep=Age);
run;

proc format;
 value Numericband 
 &low - < &high = "&low - &high"
 other = '***';
run; 

proc print data= &dsn;
format age Numericband.;
run;

%mend numeric_band_check;
%numeric_band_check (indsn=sashelp.class, dsn=test); 

 

View solution in original post

14 REPLIES 14
LinusH
Tourmaline | Level 20

Why do you think that you need a macro?

This is a perfect use case for a user defined format (see PROC FORMAT in the doc).

Data never sleeps
RTelang
Fluorite | Level 6

i have huge data so need a generic macro to code so asking help to code it in a macro.

LinusH
Tourmaline | Level 20

That statement makes no sense to me, why would a macro be more efficient than a format?

Perhaps you need to describe your constraints and requirements in more detail.

Data never sleeps
RTelang
Fluorite | Level 6
sorry i mean to say i need a macro to code age values if the age value is between the ranges then display the range if the age value is out of range then display $$$.
RW9
Diamond | Level 26 RW9
Diamond | Level 26

As @LinusH has said, there is no need for macro, macro language is for generating Base SAS code - it is not code in itself.  There is nothing at all in your post to indicate the need for generated code, it is a very basic logic gate scenario:

result=ifc(12 <= age <= 15,"12-15","$$$");

 

Alternatively you can use formats as suggested, however personally with the whole 32/64 debacle I avoid any form of proprietary compiled file where at all possible.

 

Another alternative is to have a dataset containing your values and merge that on, if there are a lot of them.

Peter_C
Rhodochrosite | Level 12
a merge is kinda difficult against a range, isn't it?
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Simple as any other merge, example:

data ranges;
  low=12; high=15; result="12-15";
run;

data have;
  age=10; output;
  age=14; output;
run;

proc sql;
  create table WANT as 
  select  A.*,
          COALESCE(B.RESULT,"$$$") as RESULT
  from    HAVE A
  left join RANGES B
  on      B.LOW <= A.AGE <= B.HIGH;
quit;
Peter_C
Rhodochrosite | Level 12
aaah - a join is ok
Reeza
Super User

Proc format;

valie age_fmt

12 -15 = '12-15'

other = $$$';

run;

 

age15 = put(age, age_fmt.);

RTelang
Fluorite | Level 6

@Reeza above i have posted my code but i want to make this code a generic code so that it can take parameters to accept the low range and high range and see if the value is within the range then display the exact range but if the value is less than or greater than the range then display '$$$'. how do i make this code generic... can u please suggest a solution.

ballardw
Super User

If you are taking parameters for the macro then why didn't you show them in your macro.

Also the way  you have constructed this format, even as an example
proc format;
value Numericband

10 - <12 = '11 - 12'
15 - <25 = '16 - 21'   (why do you confuse 25 and 21 here by the way)
other = '***';
run;

actually has 2 ranges. So if you give a single High and single low value how do you construct 2 ranges?

This is an example of creating a single range with a format

 

%macro numeric_band_check (indsn=, dsn=, low=, high=);
data &dsn;
set &indsn (Keep=Age);
run;

proc format;
 value Numericband 
 &low - < &high = "&low - &high"
 other = '***';
run; 

proc print data= &dsn;
format age Numericband.;
run;

%mend numeric_band_check;
%numeric_band_check (indsn=sashelp.class, dsn=test); 

 

RTelang
Fluorite | Level 6
@ballard thanks for the input but there another parameter variable= which is to be incorporated in the macro age shouldn't be hard coded how do it.
Reeza
Super User

@RTelang playing 20 questions isn't efficient. 

 

How do you plan to pass/generate these boundaries? Is this an attempt at creating indicator variables?

ballardw
Super User

@RTelang wrote:
@ballard thanks for the input but there another parameter variable= which is to be incorporated in the macro age shouldn't be hard coded how do it.

It looks like time to read up the documtentation on parameters. If you have a parameter for your input data set, output and see how to use that then why is it difficult to pick your paramenter name and use it where AGE is currently in the code?

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 14 replies
  • 1734 views
  • 3 likes
  • 6 in conversation