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

Hi,

 

Im new in the SAS EG environment. 

I have a Matlab code which is a format generator.

Is it possible to a build  a  SAS code which will create the same output as the given Matlab code?

 

OR

 

Can we build custom bins in SAS EG?

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

The answer then is no, not easily.  If you have the if statements (and do avoid coding all in upper case), then you could write a code parser which would read in the text, work out where low and high values are, however there is nothing in the if statements to indicate what that group should be assigned to, i.e. we could write a code parser which from:

if missing(loantovalue) then do;

can find that missing values is one group, but what to assign to this group?  So no, you can't do that.  Also, writing the code parser would be far more work than just typing the proc format statement in.

What would be better is to go the other way.  Create the format and use the format to select the if statement:

proc format;
  value b_loantovalue
...;
run;

data want;
  set have;
  select(put(loantovalue,b_loadtovalue.));
    when("Missing") do;
      scorecard_points=sum(scorecard_points,21);
      scr_loantovalue=21;
    end;
    when("Low -<0")...;
    otherwise;
  end;
run;

View solution in original post

8 REPLIES 8
Kurt_Bremser
Super User

Since you already seem to work in SAS, please do the following to illustrate your issue/task:

 

Supply some example data, preferrably as a data step with datalines (allows exact recreation of your data with copy/paste and submit).

(a macro provided in https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will automatically create a data step from a dataset)

Show the expected result.

 

You can build custom formats with proc format, and use the formatted values in statistic procedures for group processing.

Existing data can be used to build those formats automatically by creating a "cntlin" dataset for proc format.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

I am afraid your topic, and lack of examples, is too vague to provide any concrete help, so at a guess:

Is it possible to a build  a  SAS code which will create the same output as the given Matlab code? 

  It is possible to do a task which is in one programming language in another.  Whether you have the skills to do so...

 

Can we build custom bins in SAS EG?

  Yes, proc rank jumps to mind, or searching the forums:

https://communities.sas.com/t5/forums/searchpage/tab/message?advanced=false&allow_punctuation=false&...

 

I have a Matlab code which is a format generator.

  I don't know what you mean by this but SAS has formats.

sas_enthu
Calcite | Level 5

Thanks for the responses, I will try to be a bit more specific.

 

I have a huge dataset of which once of the variable is called LoanToValue, it contains 64,280 obs.

 

I would like to create a code that will create the following output:

 

value B_LOANTOVALUE

 

. = "Missing"
Low -< 0 = "Low -< 0"
0 -< 0.61 = "0 -< 0.61"
0.61 -< 0.72 = " 0.61 -< 0.72"
0.72 -< 0.77 = "0.72 -< 0.77"
0.77 -< 0.85 = "0.77 -< 0.85"
0.85 -< 0.93 = "0.85 -< 0.93"
0.93 - High = "0.93 - High";

 

Is is possible?

 

I hope I could be more clear than before.

Kurt_Bremser
Super User

So you already have working code for the proc format. Just assign the format to your loan variable; in case a procedure can not be brought to work with the formatted value, just create a new variable and use that for grouping:

proc format library=work;
value B_LOANTOVALUE
  . = "Missing"
  Low -< 0 = "Low -< 0"
  0 -< 0.61 = "0 -< 0.61"
  0.61 -< 0.72 = "0.61 -< 0.72"
  0.72 -< 0.77 = "0.72 -< 0.77"
  0.77 -< 0.85 = "0.77 -< 0.85"
  0.85 -< 0.93 = "0.85 -< 0.93"
  0.93 - High = "0.93 - High"
;
run;

data test;
input loan;
loan_formatted = loan;
format loan_formatted B_LOANTOVALUE.;
loan_string = put(loan,B_LOANTOVALUE.);
cards;
.
-1
.5
.75
.8
.9
.95
;
run;

proc print data=test noobs;
run;

Result:

         loan_
 loan    formatted       loan_string

  0      Missing         Missing     
-1.00    Low -< 0        Low -< 0    
 0.50    0 -< 0.61       0 -< 0.61   
 0.75    0.72 -< 0.77    0.72 -< 0.77
 0.80    0.77 -< 0.85    0.77 -< 0.85
 0.90    0.85 -< 0.93    0.85 -< 0.93
 0.95    0.93 - High     0.93 - High 
sas_enthu
Calcite | Level 5

Thanks but i dont have the bins in the above format. What i have is:

 

*------------------------------------------------------------*;
* Variable: LoanToValue;
*------------------------------------------------------------*;
if MISSING(LoanToValue) then do;
SCORECARD_POINTS = SCORECARD_POINTS + 21;
SCR_LoanToValue= 21;
end;
else if LoanToValue < 0 then do;
SCORECARD_POINTS = SCORECARD_POINTS + 21;
SCR_LoanToValue= 21;
end;
else if NOT MISSING(LoanToValue) and 0 <= LoanToValue AND LoanToValue < 0.51 then do;
SCORECARD_POINTS = SCORECARD_POINTS + 70;
SCR_LoanToValue = 70;
end;
else if NOT MISSING(LoanToValue) and 0.51 <= LoanToValue AND LoanToValue < 0.67 then do;
SCORECARD_POINTS = SCORECARD_POINTS + 55;
SCR_LoanToValue = 55;
end;
else if NOT MISSING(LoanToValue) and 0.67 <= LoanToValue AND LoanToValue < 0.81 then do;
SCORECARD_POINTS = SCORECARD_POINTS + 38;
SCR_LoanToValue = 38;
end;
else if NOT MISSING(LoanToValue) and 0.81 <= LoanToValue then do;
SCORECARD_POINTS = SCORECARD_POINTS + 21;
SCR_LoanToValue = 21;
end;

 

 

Currently, I am manually putting these statements in the above format, My ques is, can i generate a code that will give the output in the given format:

 

Value B_LOANTOVALUE

. = "Missing"
low-<0 = "low-<0"
0-<0.51 = "0-<0.51"
0.51-<0.67 = "0.51-<0.67"
0.67-<0.81 = "0.67-<0.81"
0.81-high = "0.81-high";

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Sorry, still not understanding, what is the question?  You know how to make a format:

proc format;
  value xyz 
    .="Missing"
...
run;

So are you saying you want to create that code from existing if statements, or are the if statements something you have tried? 

sas_enthu
Calcite | Level 5

Sorry for the trouble.

 

The if statements I mentioned is what I have from where I manually creating the following format:

value B_LOANTOVALUE
. = "Missing"
Low -< 0 = "Low -< 0"
0 -< 0.61 = "0 -< 0.61"
0.61 -< 0.72 = "0.61 -< 0.72"
0.72 -< 0.77 = "0.72 -< 0.77"
0.77 -< 0.85 = "0.77 -< 0.85"
0.85 -< 0.93 = "0.85 -< 0.93"
0.93 - High = "0.93 - High"
;
run;

 

 

My question is whether I can create any code that will automatically give me the above format code (in blue) from the if statements.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

The answer then is no, not easily.  If you have the if statements (and do avoid coding all in upper case), then you could write a code parser which would read in the text, work out where low and high values are, however there is nothing in the if statements to indicate what that group should be assigned to, i.e. we could write a code parser which from:

if missing(loantovalue) then do;

can find that missing values is one group, but what to assign to this group?  So no, you can't do that.  Also, writing the code parser would be far more work than just typing the proc format statement in.

What would be better is to go the other way.  Create the format and use the format to select the if statement:

proc format;
  value b_loantovalue
...;
run;

data want;
  set have;
  select(put(loantovalue,b_loadtovalue.));
    when("Missing") do;
      scorecard_points=sum(scorecard_points,21);
      scr_loantovalue=21;
    end;
    when("Low -<0")...;
    otherwise;
  end;
run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 8 replies
  • 1786 views
  • 2 likes
  • 3 in conversation