Desktop productivity for business analysts and programmers

Convert Matlab code to SAS

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

Convert Matlab code to SAS

[ Edited ]

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?


Accepted Solutions
Solution
‎11-27-2017 06:46 AM
Super User
Super User
Posts: 9,211

Re: SAS Codes

Posted in reply to sas_enthu

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


All Replies
Super User
Posts: 9,574

Re: SAS Codes

Posted in reply to sas_enthu

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Super User
Posts: 9,211

Re: SAS Codes

Posted in reply to sas_enthu

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.

Occasional Contributor
Posts: 5

Re: SAS Codes

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.

Super User
Posts: 9,574

Re: SAS Codes

Posted in reply to sas_enthu

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 
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Occasional Contributor
Posts: 5

Re: SAS Codes

Posted in reply to KurtBremser

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

Super User
Super User
Posts: 9,211

Re: SAS Codes

Posted in reply to sas_enthu

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? 

Occasional Contributor
Posts: 5

Re: SAS Codes

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.

Solution
‎11-27-2017 06:46 AM
Super User
Super User
Posts: 9,211

Re: SAS Codes

Posted in reply to sas_enthu

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;
☑ This topic is solved.

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

Discussion stats
  • 8 replies
  • 227 views
  • 2 likes
  • 3 in conversation