DATA Step, Macro, Functions and more

program loops and renumerative counting

Accepted Solution Solved
Reply
Frequent Contributor
Frequent Contributor
Posts: 139
Accepted Solution

program loops and renumerative counting

Hello all-

I have the following code below-

data uppy;

input spec $1. @3 pt 1.;

datalines;

a 0

a 0

a 0

a 1

a 1

a 1

;

run;

data upgraded_out;

set uppy;

by spec;

PTX=LAG(PT);

IF  FIRST.spec then do;

PTX=. ;

END;

UPGRADED=sum(PT,PTX);

run;

The results are as shown:

ObsspecptPTXUPGRADED
                            a   0 .     0
                            a   0 0     0
                            a   0 0     0
                            a   1 0     1
                            a   1 1     2
                            a   1 1     2

The problem is observation 6-I would like the end result to be the total points -3;

I cannot simply sum the values in a proc sql statement as in the end I actually need to output where the upgrade value is only two.

Any assistance greatly appreciated.

Lawrence


Accepted Solutions
Solution
‎06-04-2015 01:01 PM
Valued Guide
Posts: 860

Re: program loops and renumerative counting

This might help but I'm not sure what you are trying to accomplish:

data upgrade;

set uppy;

retain upgraded;

by spec pt;

if first.pt then upgraded = pt;

if not first.pt then upgraded = upgraded+pt;

run;

View solution in original post


All Replies
Valued Guide
Posts: 860

Re: program loops and renumerative counting

Can you add some explanation of why it should be -3?  I don't understand what you are trying to do.

Frequent Contributor
Frequent Contributor
Posts: 139

Re: program loops and renumerative counting

Posted in reply to Steelers_In_DC

Thanks Mark & Astounding! That works.

Mark-The final observation should have been 3 and not -3.  That was a typo.

In short I will explain-

What the zeros and ones represent are a patient's assessment in an ICU-

0 is not mobile, 1 is mobile. I have a physician who wants data on when a patient is mobile on the second assessment.

And I chose Mark's answer as correct as it was the first one posted (that worked)  but they both do the job! 

Lawrence

Solution
‎06-04-2015 01:01 PM
Valued Guide
Posts: 860

Re: program loops and renumerative counting

This might help but I'm not sure what you are trying to accomplish:

data upgrade;

set uppy;

retain upgraded;

by spec pt;

if first.pt then upgraded = pt;

if not first.pt then upgraded = upgraded+pt;

run;

Super User
Posts: 5,516

Re: program loops and renumerative counting

It sounds like you need something very straightforward:

data upgraded_out;

set uppy;

by spec;

if first.spec then upgraded = pt;

else upgraded + pt;

run;

Is that the result you are looking for?

Mark ... same idea looks like you posted a minute before me!

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 258 views
  • 3 likes
  • 3 in conversation