01-25-2018
ss59
Obsidian | Level 7
Member since
12-15-2017
- 42 Posts
- 15 Likes Given
- 3 Solutions
- 3 Likes Received
-
Latest posts by ss59
Subject Views Posted 1613 01-25-2018 06:17 AM 1481 01-25-2018 06:15 AM 20989 01-22-2018 07:07 AM 4360 01-18-2018 07:50 AM 4402 01-18-2018 06:27 AM 4427 01-18-2018 05:56 AM 3003 01-04-2018 12:02 PM 3038 01-04-2018 11:33 AM 6397 01-02-2018 02:49 PM 6433 01-02-2018 12:23 PM -
Activity Feed for ss59
- Liked Re: proc sql into with a where clause for Rhys. 01-25-2018 09:56 AM
- Posted Re: proc sql into with a where clause on SAS Programming. 01-25-2018 06:17 AM
- Posted proc sql into with a where clause on SAS Programming. 01-25-2018 06:15 AM
- Liked Re: macro/loop to concatenate multiple datasets - proc append? for Kurt_Bremser. 01-25-2018 06:08 AM
- Liked Re: macro/loop to concatenate multiple datasets - proc append? for s_lassen. 01-25-2018 06:08 AM
- Posted macro/loop to concatenate multiple datasets - proc append? on SAS Programming. 01-22-2018 07:07 AM
- Liked Re: sas macro - increment variable with a condition for Kurt_Bremser. 01-21-2018 06:19 AM
- Liked Re: sas macro - increment variable with a condition for Kurt_Bremser. 01-21-2018 06:19 AM
- Liked Re: sas macro - increment variable with a condition for Astounding. 01-21-2018 06:19 AM
- Posted Re: sas macro - increment variable with a condition on SAS Programming. 01-18-2018 07:50 AM
- Posted Re: sas macro - increment variable with a condition on SAS Programming. 01-18-2018 06:27 AM
- Posted sas macro - increment variable with a condition on SAS Programming. 01-18-2018 05:56 AM
- Posted Re: Summing multiple columns of matrix by column name sas iml on SAS/IML Software and Matrix Computations. 01-04-2018 12:02 PM
- Liked Re: Summing multiple columns of matrix by column name sas iml for snoopy369. 01-04-2018 12:01 PM
- Posted Summing multiple columns of matrix by column name sas iml on SAS/IML Software and Matrix Computations. 01-04-2018 11:33 AM
- Posted Re: Writing to a data table from a matrix within a do loop - iml on SAS/IML Software and Matrix Computations. 01-02-2018 02:49 PM
- Posted Re: Writing to a data table from a matrix within a do loop - iml on SAS/IML Software and Matrix Computations. 01-02-2018 12:23 PM
- Posted Re: Writing to a data table from a matrix within a do loop - iml on SAS/IML Software and Matrix Computations. 01-02-2018 11:51 AM
- Posted Re: Writing to a data table from a matrix within a do loop - iml on SAS/IML Software and Matrix Computations. 01-02-2018 11:43 AM
- Posted Re: Writing to a data table from a matrix within a do loop - iml on SAS/IML Software and Matrix Computations. 01-02-2018 11:41 AM
-
Posts I Liked
Subject Likes Author Latest Post 1 1 1 1 1 -
My Liked Posts
Subject Likes Posted 2 12-28-2017 05:51 AM 1 12-19-2017 06:03 PM
01-25-2018
06:17 AM
Figured it out. where clause should go at the end. proc sql noprint;
select distinct FieldName into : male_name_list separated by ',' from master_data where male = 1;
quit;
%put &male_name_list;
data results_final;
set results_final;
summary_male= sum(&male_name_list);
run;
... View more
01-25-2018
06:15 AM
Hi all, I'm trying to select variable names and want to sum them to create a new variable. Following code works: proc sql noprint;
select distinct FieldName into : name_list separated by ',' from master_data;
quit;
%put &name_list;
data results_final;
set results_final;
summary = sum(&name_list);
run; Now I want to do the same thing with an additional condition that involves a where clause. This is not working. could you please help? proc sql noprint;
select distinct FieldName where male = 1 into : male_name_list separated by ',' from master_data;
quit;
%put &male_name_list;
data results_final;
set results_final;
summary_male= sum(&male_name_list);
run;
... View more
01-22-2018
07:07 AM
Hi, I have a very simple problem, but it's bugging me. I have multiple datasets with same number of columns and column name that I want to merge vertically. The following code works: data results;
set result_period_1 result_period_2 result_period_3;
run; However, total no of periods is a lot, and I want to have a simple macro/loop that achieves the same thing. I tried proc append, but didn't work: %macro append;
proc append base=results_period_1 data=
%do i = 1 %to 3;
results_period_&i
%end;
run;
%mend append;
%append; Appreciate your help!
... View more
01-18-2018
07:50 AM
@Kurt_Bremser, @RW9, this is not working as expected. But I have a more basic question. I have variable "year" as a string "2017". "2018" etc and variable "period" as a number 1,2,3 or 4. I want to create a date (sas date format) with yyyyq or yyyyqq. I'm trying the following but not working: data credit_risk_2;
set credit_risk;
period_new=put(period,2.);
drop period;
rename period_new = period;
newdate = put(cats(year,period), YYQN5.);
date = intnx("QTR", newdate, 0);
format newdate yyq.;
run; What am I doing wrong?
... View more
01-18-2018
06:27 AM
@RW9, @Kurt_Bremser To be more clear, this is what I am trying to do: I have a file called Credit_risk_20164. Parameter file has all the relevant periods but I need to update the variables from the credit_risk file for the next period and merge in relevant parameters from the next period. I want to generate projections from 20171 to 20184 %macro projections(year_period);
%DO i = 1 %TO 8;
%let if mod(year_period,10) LE 1 then prevyear_period = %eval(year_period -7);
%let if mod(year_period,10) > 1 then prevyear_period = %eval(year_period -1);
/***increment time in credit risk***/
data credit_risk_&year_period;
set credit_risk_&prevyear_period;
if mod(year_period,10) LE 3 then year_period_new = year_period +&i;
if mod(year_period,10) > 3 then year_period_new = year_period +6+&i;
drop year_period;
rename year_period_new = year_period;
run;
/****join it with parameters for that time period***/
proc sql ;
create table credit_risk_&year_period as
select a.* , b.*
from credit_risk_&year_period as a left join parameters as b
on a.country = b.country and a.segment_combo = b.segment_combo and a.year_period = b.year_period and a.IND_CRE_IMPAIRMENT = b.IND_CRE_IMPAIRMENT and a.CRE_LTV_BAND = b.CRE_LTV_BAND and a.CRE_GEOGRAPHY_ZONE = b.CRE_GEOGRAPHY_ZONE;
quit;
/**********calculation of stocks*************/
/**********output the end state results for the period*******/
proc export
data=credit_risk_&year_period
dbms=XLSX
outfile="C:\Users\credit_risk"
replace; SHEET="Results_&year_period";
run;
/*********increment the period*********/
%let if mod(year_period,10) LE 3 then year_period = %eval(year_period +1);
%let if mod(year_period,10) > 3 then year_period = %eval(year_period +7);
%END;
%mend projections;
projections(20171);
/*********should give me results till 20184 and separate sheets in the credit_risk.xlsx file************/
... View more
01-18-2018
05:56 AM
Hi all, I'm trying to write a SAS macro for a time series and my time variable is "year_period", i.e. 20171, 20172, 20173, 20174, 20181, ..... I'm having a hard time to increment the variable. I'm trying to accomplish something like: %macro projections(year_period);
%DO i = 1 %TO 8;
/********code block*****/
%let if mod(year_period,10) LE 3 then year_period = %eval(year_period +1);
%let if mod(year_period,10) > 3 then year_period = %eval(year_period +7);
%END;
%mend projections; But the %let and %eval statements are not correct.Could you please help?
... View more
01-04-2018
12:02 PM
That is quite neat. Thanks!
... View more
01-04-2018
11:33 AM
Hi all, I have a nxn matrix (for simplicity assuming n=3) like following: proc iml;
A = {0.1 0.2 0.5, 0.2 0.4 0.6, 0.2 0.3 0.8}; I have another table - a reference table, which specifies which values from this nxn (n=3) matrix needs to be written to a new data table. that table is following: data output_table; infile datalines dsd; length FieldName $20 FieldSourceTable $20; input Matrix_Id Row_Order Column_Order IsZero FieldName FieldSourceTable; datalines; 3, 1, 1, 1, ., . 3, 1, 2, 0, result1, table2 3, 1, 3, 0, result1, table2 3, 2, 1, 1, ., . 3, 2, 2, 0, sum1, table2 3, 2, 3, 0, sum2, table2 3, 3, 1, 0, prod1, table2 3, 3, 2, 0, prod2, table2 3, 3, 3, 1, ., . ; Assume the matrix A is my Matrix_ID 3. So, I need to create a new table called table 2 with the names specified in 'FieldName' column in the reference table and write specific values from the matrix to the table (e.g. (2,3)th element to the column 'sum2'). The following code works for the purpose: proc sort data= output_table out= output_table;
by Row_Order Column_Order;
quit;
proc iml;
use output_table; /* parameterization table for matrix 3 (output table)*/
read all var {Column_Order Row_Order FieldName};
close;
idx = loc(FieldName ^= " ");
refNames = FieldName[idx];
A = {0.1 0.2 0.5, 0.2 0.4 0.6, 0.2 0.3 0.8}; /*A is the full matrix to write*/
C = A[idx];
C = t(C);
mattrib C colname=refNames;
print C; The ouput matrix C looks like following: result1 result1 sum1 sum2 prod1 prod2 0.2 0.5 0.4 0.6 0.2 0.3 What I want to do is, whenever I have two or more elements of the matrix to be written to the same row, I want them to be summed up. So, my ideal output in this case would be to sum up 0.2 and 0.5 and have 0.7 as result1 as the following output matrix (let's call it D): result1 sum1 sum2 prod1 prod2 0.7 0.4 0.6 0.2 0.3 Then I'd write the matrix D to a data table: create data_to_write from D[colname = refNames]; append from D; /** create data set and append in each subsequent steps**/ end; Not being able to sum the elements with same column name to create the matrix D for all cases of same column name. Could you please help?
... View more
01-02-2018
02:49 PM
Hi @kiranv_, if you look at the param_table for Matrix_ID 2, it looks like following: FieldName FieldSourceTable Matrix_Id Row_Order Column_Order IsZero IsRowResidual IsColumnResidual 2 1 1 1 0 0 result1 table2 2 1 2 0 0 0 result2 table2 2 1 3 0 0 0 2 2 1 1 0 0 sum1 table2 2 2 2 0 0 0 sum2 table2 2 2 3 0 0 0 prod1 table2 2 3 1 0 0 0 prod2 table2 2 3 2 0 0 0 2 3 3 1 0 0 My desired table2 would be: result1 result2 sum1 sum2 prod1 prod2 5 2 6 6 19.8 3.3 5 1 8 4 19.8 6.6 The first row comes from the first EAD_flows matrix, second row comes form the second matrix (with desired elements as mentioned in param_table). I can do it for one row, but seeking help to do it in the loop so that I get the dataset table2 with multiple rows corresponding to each Ead_flow matrix
... View more
01-02-2018
12:23 PM
@kiranv_ - that results in only the last matrix to be written to the data. Only one row. The other matrices in the loop are not captured. If you do print EAD_flows; before the end; you'll see these two matrices. I want one row for each matrix in the table2 data through the loop. EAD_flows 3 5 2 0 6 6 19.8 3.3 9.9 EAD_flows 4 5 1 0 8 4 19.8 6.6 6.6
... View more
01-02-2018
11:51 AM
@Reeza yes, sorry about that. But even the modified code does not serve my purpose. I want something like this (and the code below does not work) : /********create parametrisation table*********/
data param_table;
infile datalines dsd;
length FieldName $20 FieldSourceTable $20;
input Matrix_Id Row_Order Column_Order IsZero IsRowResidual IsColumnResidual FieldName FieldSourceTable;
datalines;
1, 1, 1, 0, 1, 0, ., .
1, 1, 2, 0, 0, 0, xyz, table1
1, 1, 3, 0, 0, 0, abc, table1
1, 2, 1, 1, 0, 0, ., .
1, 2, 2, 0, 0, 0, pqr, table1
1, 2, 3, 0, 0, 0, mno, table1
1, 3, 1, 0, 0, 0, ab, table1
1, 3, 2, 0, 0, 0, pq, table1
1, 3, 3, 0, 1, 0, ., .
2, 1, 1, 1, 0, 0, ., .
2, 1, 2, 0, 0, 0, result1, table2
2, 1, 3, 0, 0, 0, result2, table2
2, 2, 1, 1, 0, 0, ., .
2, 2, 2, 0, 0, 0, sum1, table2
2, 2, 3, 0, 0, 0, sum2, table2
2, 3, 1, 0, 0, 0, prod1, table2
2, 3, 2, 0, 0, 0, prod2, table2
2, 3, 3, 1, 0, 0, ., .
;
proc sort data= param_table out= param_table;
by Matrix_Id Row_Order Column_Order;
quit;
proc sql;
create table param_tr as
select * from work.param_table
where Matrix_Id = 1;
quit;
proc sql;
create table param_flow as
select * from work.param_table
where Matrix_Id = 2;
quit;
/********create data table*********/
data table1;
input Year (country method Segment) ( : $12.)
ABC XYZ PQR MNO AB PQ;
datalines;
2017 France ABC Retail 0.2 0.5 0.4 0.3 0.6 0.1
2017 France XYZ Corporate 0.1 0.5 0.4 0.2 0.6 0.2
;
run;
/*********build matrices and write out*********/
proc iml;
use param_tr;
read all var {Column_Order Row_Order FieldName};
close;
idx = loc(FieldName ^= " ");
refNames = FieldName[idx];
use param_flow;
read all var {Column_Order Row_Order FieldName};
close;
idx_2 = loc(FieldName ^= " ");
refNames_2 = FieldName[idx_2];
use table1;
read all var refNames into Y;
close;
/******Create 3x3 EAD transition matrix for each row of table 1*****/
do i = 1 to nrow(Y);
TR_MAT = j(3,3,0);
TR_MAT[idx_2] = Y[i,];
temp = J(3,1)- TR_MAT[,+];
do j=1 to 3;
TR_MAT[j,j] = temp[j,1];
end;
EAD_INIT = {10, 20, 33};
EAD_flows=(EAD_INIT#TR_MAT);
/**********write elements of EAD flow out*********/
EAD_flow_MAT = EAD_flows[idx_2];
EAD_flow_MAT = t(EAD_flow_MAT);
mattrib EAD_flow_MAT colname=refNames_2;
create table2 from EAD_flow_MAT[colname = refNames_2];
append from EAD_flow_MAT; /** create data set and append in each subsequent steps**/
end;
close table2;
... View more
01-02-2018
11:43 AM
@Reeza when I specify i =1 or 2, I get the desired output in dataset table2. I am trying to have the dataset open and append to it in each run of the loop and get as many rows as table1 as an end result.
... View more
01-02-2018
11:41 AM
@Reeza this code runs in my machine in the current form. Could you please clarify what error you are getting? I'm trying to resolve this section: create table2 from EAD_flow_MAT[colname = refNames_2]; append from EAD_flow_MAT; /** create data set **/ close table2; I want have the do loop, create the dataset table2 in the first go of the loop, and then append to it in each subsequent steps of he do loop. Please let me know if that was not clear.
... View more
01-02-2018
11:26 AM
Hi, I have a parametrisation table and a data table. For each row of data table, I am creating a transition matrix, multiplying that matrix to a constant matrix element-wise and trying to write specific elements of output matrix to a new table. The following code works perfectly without the do loop, i.e. it only works for one row of table1 at a time. The writing ("create" and "append" statement within a do loop) does not work when I have the do loop. Ideally I want for one run of the loop a new row will be appended to the table2 that I am creating. /********create parametrisation table*********/ data param_table; infile datalines dsd; length FieldName $20 FieldSourceTable $20; input Matrix_Id Row_Order Column_Order IsZero IsRowResidual IsColumnResidual FieldName FieldSourceTable; datalines; 1, 1, 1, 0, 1, 0, ., . 1, 1, 2, 0, 0, 0, xyz, table1 1, 1, 3, 0, 0, 0, abc, table1 1, 2, 1, 1, 0, 0, ., . 1, 2, 2, 0, 0, 0, pqr, table1 1, 2, 3, 0, 0, 0, mno, table1 1, 3, 1, 0, 0, 0, ab, table1 1, 3, 2, 0, 0, 0, pq, table1 1, 3, 3, 0, 1, 0, ., . 2, 1, 1, 1, 0, 0, ., . 2, 1, 2, 0, 0, 0, result1, table2 2, 1, 3, 0, 0, 0, result2, table2 2, 2, 1, 1, 0, 0, ., . 2, 2, 2, 0, 0, 0, sum1, table2 2, 2, 3, 0, 0, 0, sum2, table2 2, 3, 1, 0, 0, 0, prod1, table2 2, 3, 2, 0, 0, 0, prod2, table2 2, 3, 3, 1, 0, 0, ., . ; proc sort data= param_table out= param_table; by Matrix_Id Row_Order Column_Order; quit; proc sql; create table param_tr as select * from work.param_table where Matrix_Id = 1; quit; proc sql; create table param_flow as select * from work.param_table where Matrix_Id = 2; quit; /********create data table*********/ data table1; input Year (country method Segment) ( : $12.) ABC XYZ PQR MNO AB PQ; datalines; 2017 France ABC Retail 0.2 0.5 0.4 0.3 0.6 0.1 2017 France XYZ Corporate 0.1 0.5 0.4 0.2 0.6 0.2 ; run; /*********build matrices and write out*********/ proc iml; use param_tr; read all var {Column_Order Row_Order FieldName}; close; idx = loc(FieldName ^= " "); refNames = FieldName[idx]; use param_flow; read all var {Column_Order Row_Order FieldName}; close; idx_2 = loc(FieldName ^= " "); refNames_2 = FieldName[idx_2]; use table1; read all var refNames into Y; close; /******Create 3x3 EAD transition matrix for each row of table 1*****/ /*do i = 1 to nrow(Y);*/ /*I want to include the loop*/ TR_MAT = j(3,3,0); TR_MAT[idx_2] = Y[2,]; /*this should be Y[i,]*/ temp = J(3,1)- TR_MAT[,+]; do j=1 to 3; TR_MAT[j,j] = temp[j,1]; end; EAD_INIT = {10, 20, 33}; EAD_flows=(EAD_INIT#TR_MAT); /**********write elements of EAD flow out*********/ EAD_flow_MAT = EAD_flows[idx_2]; EAD_flow_MAT = t(EAD_flow_MAT); mattrib EAD_flow_MAT colname=refNames_2; create table2 from EAD_flow_MAT[colname = refNames_2]; append from EAD_flow_MAT; /** create data set **/ close table2; /*end;*/ *******I want to have the do loop How can I write the result of each row table1 to different rows of "table2" within a loop? Thanks!
... View more
12-28-2017
05:51 AM
2 Likes
I've been able to figure this out: proc sort data= output_table out= output_table;
by Row_Order Column_Order;
quit;
proc iml;
use output_table; /* parameterization table for matrix 3 (output table)*/
read all var {Column_Order Row_Order FieldName};
close;
idx = loc(FieldName ^= " ");
refNames = FieldName[idx];
A = {0.1 0.2 0.5, 0.2 0.4 0.6, 0.2 0.3 0.8}; /*A is the full matrix to write*/
C = A[idx];
C = t(C);
mattrib C colname=refNames;
print C;
create data_to_write from C[colname = refNames];
append from C; /** create data set **/
close data_to_write;
... View more