DATA Step, Macro, Functions and more

do loop value assignment not working

Reply
N/A
Posts: 0

do loop value assignment not working

I have the following data structure:

value var_name varnum valnum
horse animal 1 1
cat animal 1 2
dog animal 1 3
high priority 2 1
low priority 2 2


I have coded the following:

%let neqtn=3;

data all_map;
do _n_=1 until (last.valnum);
betaval=1;
set total ;
by varnum valnum;
if last.varnum ne 1 then do;
betaval=betaval +(&neqtn-1);
retain betaval;
end;
else do;
betaval=0;
end;

end;
run;

This produces the following result:

value var_name varnum valnum betaval
horse animal 1 1 3
cat animal 1 2 3
dog animal 1 3 0
high priority 2 1 2
low priority 2 2 0

This is what I want it to produce:

value var_name varnum valnum betaval
horse animal 1 1 B3
cat animal 1 2 B5
dog animal 1 3 -sum(B3+B5)
high priority 2 1 B7
low priority 2 2 -B7

etc. I am assigning the reference to the beta value not the actual value at this point so the beta value for the last obs of a variable will still only be a reference to multiple beta values(from previous observations). Also, they are incremented by a known value, neqtn-1( number of equations -1), in this case 2. How do I retain the betaval across variable groups to allow incrementing to correct beta? It resets for each new variable. How do I assign betaval to last obs in a group appropriately(my current code sets last obs beta val=0, short of a solution)?
Super Contributor
Super Contributor
Posts: 3,174

Re: do loop value assignment not working

Posted in reply to deleted_user
To start, you are using a SAS-reserved variable _N_ which is controlled by the SET statement. Suggest you might add some additional diagnostic statements for your own debugging opportunity, such as:

PUTLOG '>DIAG00' /_ALL_;


Scott Barry
SBBWorks, Inc.
N/A
Posts: 0

Re: do loop value assignment not working

Posted in reply to deleted_user
Thanks for the tip. The diagnostics are helpful. I know the code cannot accomplish what I need as written however. Any suggestions for best way to initialize betaval or to retain values for last obs.

Thanks.
Super Contributor
Super Contributor
Posts: 3,174

Re: do loop value assignment not working

Posted in reply to deleted_user
Investigate the LAG function. There is also a RETAIN statement along with an assignment to allow a variable value to be captured for a subsequent DATA step pass.

Scott Barry
SBBWorks, Inc.
N/A
Posts: 0

Re: do loop value assignment not working

Posted in reply to deleted_user
Thanks Scott, I'll look into those and see if they're able to achieve what I need.

Any other suggestions are appreciated.
Super Contributor
Super Contributor
Posts: 3,174

Re: do loop value assignment not working

Posted in reply to deleted_user
You may need to generating a separate file with desired values and merge that file back against your source file. Otherwise I don't see how you can decide conditionally when/how to capture and retain values, since you need them for each pertinent observation. A pre-processing SORT and DATA step, followed by another SORT and MERGE sounds more appropriate. Scrap the idea of using LAG or RETAIN, as well.

Scott Barry
SBBWorks, Inc.
N/A
Posts: 0

Re: do loop value assignment not working

Posted in reply to deleted_user
Change the statement betaval=1; to if _n_=1 then betval=1;

~ Sukanya E
N/A
Posts: 0

Re: do loop value assignment not working

Posted in reply to deleted_user
Thanks Scott. I think you're right. I was able to correct the assignment of betaval for observations 1
N/A
Posts: 0

Re: do loop value assignment not working

Posted in reply to deleted_user
Scott I have a couple of more issues needing resolution to get the larger program to perform properly and meet a tight deadline -are you available this afternoon for some questions. They are macro and array related. - sry for posting request directly to thread - I'm not aware of another method.
Super Contributor
Super Contributor
Posts: 3,174

Re: do loop value assignment not working

Posted in reply to deleted_user
Start a new thread for each post / question -- there are others on this forum besides myself who can assist.

However, for me personally -- in about 20 minutes you will be competing with my Sunday afternoon cocktail session, and...DANG!....I still need to get the recycle and house-trash picked up before commencing iterative libation consumption exercises.

Again, there are others monitoring these forums and I'd encourage you to use them, wisely of course, after doing your own leg-work first, regardless of the specific concern.

Scott Barry
SBBWorks, Inc.
N/A
Posts: 0

Re: do loop value assignment not working

Thanks for responding to my post! I will start a thread for each issue. Hope you enjoyed your afternoon happy hour.

Cheers.
Ask a Question
Discussion stats
  • 10 replies
  • 214 views
  • 0 likes
  • 2 in conversation