RAS method

I want codes regarding performing RAS method in GAMS so that row totals and column totals remain equal so that CGE modelling can be done.

The following might be helpful.

$title RAS Balancing of SAM (2022-23)


sets
    i   'SAM accounts' /"Agriculture", "Coal", "Crude petroleum", "Natural Gas", "Mining", "Other manufacturing", 
                        "Construction", "Electricity - Solar", "Electricity - Wind", "Electricity - Hydro", 
                        "Electricity - Nuclear", "Electricity - Oil", "Electricity - Coal", "Electricity - Gas", 
                        "Electricity - Other energy sources", "Railway transport", "Land transport", "Water transport", 
                        "Air transport", "Finance and Insurance", "Other services", "Labour", "Capital", 
                        "Rural - Self employed in agriculture", "Rural- Self employed in non-agriculture", 
                        "Rural - Salaried earning in agriculture", "Rural - Salaried earning in non-agriculture", 
                        "Casual labour in agriculture", "Casual labour in non-agriculture", "Rural - Other households", 
                        "Urban - Self employed", "Urban - Salaried earning", "Urban - Casual labour", 
                        "Urban - Other households", "Rural - SC", "Rural -ST", "Rural - OBC", "Rural - Others", 
                        "Urban - SC", "Urban - ST", "Urban - OBC", "Urban - Others", "Private corporations", 
                        "Public non-departmental sector", Government, "Indirect tax", "Capital account", 
                        "Rest of the world"/;
Set tg column and row constraints /col,row/;
                       
Alias(i,j,k);  

parameters
    SAM(i,j)       'Original SAM matrix from Excel',
    TargetRow(i)   'Target row totals',
    TargetCol(j)   'Target column totals',
    RAS_SAM(i,j)   'RAS balanced SAM',
    rowFactor(i)   'Row scaling factor',
    colFactor(j)   'Column scaling factor',
    sumRow(i)      'Row sums',
    sumCol(j)      'Column sums',
    diffRow(i)     'Difference between target and current row sums'
    diffCol(j)     'Difference between target and current column sums'
    BalMat(i,j)    'Balanced Matrix'
    CHKCOLROW      'Check for column and row sum equality'
    TARGET(*,tg)   'Column & Row constraints';
    
scalar
    delta     'Small number to avoid division by zero' /1e-10/
    tol       'Convergence tolerance' /1e-6/
    iterMax   'Maximum number of iterations' /1000/
    iterCount 'Iteration counter'
    maxDiff   'Maximum difference between target and current sums'
    converged 'Convergence flag (0/1)';
    
* Load SAM from Excel via GDX
$call.checkErrorLevel gdxxrw "D:\Research work\SAM_2022-23.csv" par=SAM rng=B3:AX51 rdim=1 cdim=1 trace=2
$gdxin SAM_2022-23.gdx
$load SAM
display SAM;

* Calculate row sum and column sum
sumRow(i) = sum(j, SAM(i,j));
sumCol(j) = sum(i, SAM(i,j));

* Print them to the listing
display sumRow, sumCol;

RAS_SAM(i,j)=(SAM(i,j)/sumCol(i))$(sumCol(i) ne 0) +0.;

* Initialize balanced matrix with original SAM
RAS_SAM(i,j) = SAM(i,j);

After this how to perform RAS method as I am stuck