DATA Step, Macro, Functions and more

Display range & special character '$$$'

Accepted Solution Solved
Reply
Regular Contributor
Posts: 190
Accepted Solution

Display range & special character '$$$'

[ Edited ]

 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=);

 


Accepted Solutions
Solution
‎09-19-2016 06:57 AM
Super User
Posts: 10,487

Re: Display age range & special character '$$$'

[ Edited ]

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


All Replies
Super User
Posts: 5,256

Re: Display age range & special character '$$$'

[ Edited ]

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
Regular Contributor
Posts: 190

Re: Display age range & special character '$$$'

[ Edited ]

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

Super User
Posts: 5,256

Re: Display age range & special character '$$$'

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
Regular Contributor
Posts: 190

Re: Display age range & special character '$$$'

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 $$$.
Super User
Super User
Posts: 7,401

Re: Display age range & special character '$$$'

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.

Valued Guide
Posts: 2,174

Re: Display age range & special character '$$$'

a merge is kinda difficult against a range, isn't it?
Super User
Super User
Posts: 7,401

Re: Display age range & special character '$$$'

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;
Valued Guide
Posts: 2,174

Re: Display age range & special character '$$$'

aaah - a join is ok
Super User
Posts: 17,784

Re: Display age range & special character '$$$'

Proc format;

valie age_fmt

12 -15 = '12-15'

other = $$$';

run;

 

age15 = put(age, age_fmt.);

Regular Contributor
Posts: 190

Re: Display age range & special character '$$$'

[ Edited ]

@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.

Solution
‎09-19-2016 06:57 AM
Super User
Posts: 10,487

Re: Display age range & special character '$$$'

[ Edited ]

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); 

 

Regular Contributor
Posts: 190

Re: Display age range & special character '$$$'

@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.
Super User
Posts: 17,784

Re: Display age range & special character '$$$'

@RTelang playing 20 questions isn't efficient. 

 

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

Super User
Posts: 10,487

Re: Display age range & special character '$$$'


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?

☑ This topic is SOLVED.

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

Discussion stats
  • 14 replies
  • 503 views
  • 3 likes
  • 6 in conversation