DATA Step, Macro, Functions and more

problem in modify statement

Reply
Super Contributor
Posts: 318

problem in modify statement

data fa_sample;
id = 1;
valid_from = '01JAN2009:00:00:00'dt;
valid_to = '02FEB2009:00:00:00'dt;
format valid_from nldatm. valid_to nldatm.;
run;

data sp_sample;
id = 1;
valid_from = '01JAN2009:00:00:00'dt;
valid_to = '31DEC9999:00:00:00'dt;
format

data sp_sample;
modify sp_sample;
put valid_to=;
if _N_ = 1 then do;
declare hash h(dataset:"fa_sample");
h.definekey("id","valid_from");
h.definedata("valid_to");
h.definedone();
end;
rc = h.find();
put valid_to=; /* value is 02FEB2009:00:00:00 but when i'm viewing the dataset value is 31DEC9999:00:00:00 pa rin*/
replace;
format valid_from nldatm. valid_to nldatm.;
run;

what am i doing wrong in my data steps? the dataset is not updated

thanks a lot! Smiley Happy
Super Contributor
Posts: 318

Re: problem in modify statement

got it working already no need for replace;

the final code goes like this:

data sp_sample;
dsid=open("fa_sample","i");
dsid=close(dsid);

declare hash h(dataset:"fa_sample");
h.definekey("id","valid_from");
h.definedata("valid_to");
h.definedone();

modify sp_sample;
rc = h.find();
format valid_from nldatm. valid_to nldatm.;
run;

i just don't get it yet why i have to use the open function first. is it because the read access of modify is not the same when using set or update?

thanks!
Super Contributor
Posts: 474

Re: problem in modify statement

Actually you don't.

The only requirement is to include in the PDV (Program Data Vector) the complete dataset layout before declaring the hash. The open function will do just that, allocate in the PDV the dataset layout.

You could substitute:

dsid=open("fa_sample","i");
dsid=close(dsid);

For:

if 0 then set fa_sample;

Now, this is kind of a trick, this will set the dataset (alocate layout into the PDV) without actually reading any row from it (if 0, will always evaluate to false).

Both codes will do exactly the same.

Cheers from Portugal.

Daniel Santos @ www.cgd.pt.
Super Contributor
Posts: 318

Re: problem in modify statement

thanks for the tip Daniel. learned something new today.
Ask a Question
Discussion stats
  • 3 replies
  • 121 views
  • 0 likes
  • 2 in conversation