BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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)?
10 REPLIES 10
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.
deleted_user
Not applicable
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.
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.
deleted_user
Not applicable
Thanks Scott, I'll look into those and see if they're able to achieve what I need.

Any other suggestions are appreciated.
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.
deleted_user
Not applicable
Change the statement betaval=1; to if _n_=1 then betval=1;

~ Sukanya E
deleted_user
Not applicable
Thanks Scott. I think you're right. I was able to correct the assignment of betaval for observations 1
deleted_user
Not applicable
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.
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.
deleted_user
Not applicable
Thanks for responding to my post! I will start a thread for each issue. Hope you enjoyed your afternoon happy hour.

Cheers.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 10 replies
  • 805 views
  • 0 likes
  • 2 in conversation