arraysif-statementsasdo-loops

Do Loops (with multiple rows of id's) with conditional statements?


Please see my data below;


data finance;
  input id loan1 loan2 loan3 assets home$ type;
datalines;
1 93000 98000 45666 new 1
1 98000 45678 98765 67 old 2
1 55000 56764 435371 54 new 1
2 7000 6000 7547 57 new 1
4 67333 87444 98666 34 old 1
4 98000 68777 986465 23 new 1
5 4555 334 652 12 new 1
5 78999 98999 80000 34 new 1
5 889 989 676 3 new 1
;

data finance1;
set finance;
if loan1<80000 then conc'level1';
if loan2 <80000 and home='new' then borrowcap = 'high';
run;

I would like the following dataset, as you can see although there are multiple rows for each ID initially, if there was a level1 or high in any of those rows, I would like to capture that in the same row.

data finance;
input id conc$ borrowcap$;
datalines;
1 level1 high
2 level1 high
4 level1
5 level1 high
;

Any help is appreciated!


Solution

  • Use retain statement, you can keep value from any row for each ID. Use by statement + if last.var statement, you can keep only one row for each ID.

    data finance;
      input id loan1 loan2 loan3 assets home$ type;
    datalines;
    1 93000 98000 45666   . new 1
    1 98000 45678 98765  67 old 2
    1 55000 56764 435371 54 new 1
    2 7000  6000  7547   57 new 1
    4 67333 87444 98666  34 old 1
    4 98000 68777 986465 23 new 1
    5 4555  334   652    12 new 1
    5 78999 98999 80000  34 new 1
    5 889   989   676    3  new 1
    ;
    
    data finance1;
      set finance;
      by id;
    
      retain conc borrowcap;
      length conc borrowcap $8.;
      if first.id then call missing(conc,borrowcap);
      if loan1<80000 then conc='level1';
      if loan2<80000 and home='new' then borrowcap = 'high';
    
      if last.id;
    run;