## DATA Step, Macro, Functions and more

Solved
Contributor
Posts: 62

Hello everybody ,

i have this dataset:

I would like to have this result (if IDCAB  is blank must take the value of the previous cell):

I use this query:
data TABLE_1;

set  TABLE_1;

n=_n_;

if missing(IDCAB) then do;

do until (not missing(IDCAB));

n=n-1;

set TABLE_1(keep=IDCAB) point=n;

end;

end;

run;

it works, but with "big dataset" (such as 600,000 records) it crashes.

Please, Can you suggest an alternative?

Thank you.

Accepted Solutions
Solution
‎11-25-2011 08:03 AM
Frequent Contributor
Posts: 82

Try:

data table_2 (drop=idcab rename=idcab_mod=idcab);

set table_1;

retain idcab_mod ;

if idcab ne '' then idcab_mod =idcab;

run;

All Replies
Solution
‎11-25-2011 08:03 AM
Frequent Contributor
Posts: 82

Try:

data table_2 (drop=idcab rename=idcab_mod=idcab);

set table_1;

retain idcab_mod ;

if idcab ne '' then idcab_mod =idcab;

run;

Contributor
Posts: 62

YES!!!!!

" i love you IEVA" !!!  :smileylaugh:

thank you!!!

Contributor
Posts: 62

Sorry...!!!

I have another similar question...

i have this dataset:

I would like to have this result (if IDCAB  is blank and AID is equal to the previous cell,

IDCAB is  must take the value of the previous cell, If this is not true the cell should be blank):

This dont' work...

data TABLE_1(drop=IDCAB AID  rename=IDCAB_MOD=IDCAB rename=AID_MOD=AID);

set TABLE_2;

retain IDCAB_MOD AID_MOD ;

if (IDCAB ne '' AND AID_MOD=AID) then IDCAB_MOD =IDCAB;

run;

Thank you.

PROC Star
Posts: 8,163

For the cells that you want missing, the aid numbers are the same in your sample data, but different in your desired output.  Which is correct?

Frequent Contributor
Posts: 82

This could be the solution you are looking for:

proc sort data=table_1;

by aid descending idcab;

run;

data table_2 (drop=idcab rename=idcab_mod=idcab);

set table_1;

by aid descending idcab;

retain  idcab_mod ;

if first.aid  then idcab_mod =idcab;

run;

Super Contributor
Posts: 1,636

data have;

infile cards missover;

input aid \$ idcab \$;

cards;

1 bb

1

2 fv

2

2

2 cv

;

run;

data want (drop=idcab rename=idcab_mod=idcab);

set have;

by aid;

retain idcab_mod ;

if (first.aid or idcab ne '' )then idcab_mod =idcab;

run;

PROC Star
Posts: 8,163

Presuming that your output file contained the correct values, and that your file is already in the order shown in your example, then the following would do (I think) what you want:

data table_2 (drop=hold;

set table_1;

by aid notsorted;

retain hold_idcab;

if first.aid then hold_idcab=idcab;

else idcab=hold_idcab;

run;

Contributor
Posts: 62

sorry for my mistake, output file container the correct values.

your support is really precious !!!!

Thanks to all!!!

Super Contributor
Posts: 1,636

Dear  Art,

I ran your code. It did not have the correct outcome for the last observation.

data table_1;

infile cards missover;

input aid \$ idcab \$;

cards;

1 bb

1

2 fv

2

2

2 cv

;

run;

data table_2 (drop=hold;

set table_1;

by aid notsorted;

retain hold_idcab;

if first.aid then hold_idcab=idcab;

else idcab=hold_idcab;

run;

proc print;

run;

Obs    aid    idcab

4      2      fv

5      2      fv

6      2      fv

7      2      fv

PROC Star
Posts: 8,163

The OP didn't have such a condition in the example data, thus I decided that it would be presumptive to include one.  Obviously, if the OP wanted non-missing values were to be left alone, the decision would also have to be made regarding whether such new values should be assigned to later repititions as well.

Soapbox: Unfortunately, programmers often write code to cover conditions that are outside of the defined requirements, rather than simply mentioning the possible discrepancy(ies) and asking how it/they should be handled.

🔒 This topic is solved and locked.