DATA Step, Macro, Functions and more

assigning a value to a variable through a condition.

Reply
Contributor
Posts: 37

assigning a value to a variable through a condition.

Hello,

 

If anyone can help me with my code. I need to place an existing value to a new variable, instead it's giving me a zero value. 

 

Code:

DATA ALLC; SET ALLB;

LENGTH NPN $5;

IF (C_ID=C_IDN) AND (TDTN > PDT) THEN NPN=FIRST.PN;

ELSE NPN = PN;

RUN;

PROC PRINT N; ID C_ID; VAR CPRENDINGDT PDT TDTN NPN PN;

 

RUN;

 

My Result:  Pelase see attachement.

Thank you.

PROC Star
Posts: 7,468

Re: assigning a value to a variable through a condition.

You can't use first.pn without including a 

by pn;

statement

 

Art, CEO, AnalystFinder.com

 

Super User
Posts: 19,772

Re: assigning a value to a variable through a condition.

[ Edited ]

First.PN will always be 1 or 0, as it's an indicator if a record is the first record or not. It does not hold the value of the first value of PN which is what I think you're looking for, see the code below. I had to add, BY and RETAIN statements and modified the code to keep the first variable. 

 

Please clarify your question by describing what you want rather than code that doesn't work. 

Also, post sample data as text directly in the forum as mnay users won't work with xlsx or docx files. 

 

 

 

 

DATA ALLC; SET ALLB;
LENGTH NPN $5;
by PN;
Retain First_PN;

if first.PN then First_PN= PN;

IF (C_ID=C_IDN) AND (TDTN > PDT) THEN NPN=First_PN;
ELSE NPN = PN;
RUN;

 

PROC Star
Posts: 7,468

Re: assigning a value to a variable through a condition.

Are pdt and tdtn sas dates or just character fields containing date looking information? If they're character fields, in addition to the first. problem, your logic won't work as expected.

 

Art, CEO, AnalystFinder.com

 

Contributor
Posts: 37

Re: assigning a value to a variable through a condition.

Hello,

I didn't make my question clear, please see the attachement for more details. Thank you.

 

I need to compare the C_ID with C_IDN and if the CEDT and PDT the same than create a new varaible named NPN with the PN's value and if the PDT is less than the TDTN than I want the previous PN value for the NPN vaule. 

PROC Star
Posts: 7,468

Re: assigning a value to a variable through a condition.

You didn't answer my question: are the dates character fields or SAS dates?

 

Art, CEO, AnalystFinder.com

 

Contributor
Posts: 37

Re: assigning a value to a variable through a condition.

I am sorry Art, The date is SAS dateformat as date9. Thank you.
PROC Star
Posts: 7,468

Re: assigning a value to a variable through a condition.

OK. But your question still isn't clear. You said:

I need to compare the C_ID with C_IDN and if the CEDT and PDT the same than create a new varaible named NPN with the PN's value and if the PDT is less than the TDTN than I want the previous PN value for the NPN vaule. 

 

You mention comparing C_ID with C_IDN, but didn't say anything about what if they are equal or unequal. Are you saying that if C_ID and C_IDN are the same, and if CEDT and PDT are the same, then create the new variable equal to the PN value.

 

Do you want to do the pdt less than computation ONLY if the condition in the last paragraph isn't met?

 

Art, CEO, AnalystFinder.com

 

Contributor
Posts: 37

Re: assigning a value to a variable through a condition.

Thank you Art for your response.

I need to compare the C_ID with C_IDN and if they are equal then if the CEDT and PDT the same than create a new variable named NPN with the PN's value and if the PDT is less than the TDTN than I want the previous PN value for the NPN value.

YES, please. You mention comparing C_ID with C_IDN, but didn't say anything about what if they are equal or unequal. Are you saying that if C_ID and C_IDN are the same, and if CEDT and PDT are the same, then create the new variable equal to the PN value.



Do you want to do the pdt less than computation ONLY if the condition in the last paragraph isn't met? If the PDT less than the TDTN and if C_ID and C_IDN are the same, and if CEDT and PDT are the same, then create the new variable equal to the previous PN value.



Thank you Art.

Super User
Posts: 19,772

Re: assigning a value to a variable through a condition.

Did you try the code? What part of it was incorrect?

Contributor
Posts: 37

Re: assigning a value to a variable through a condition.

Thank you and yes, it didn't give me the previous value, it give me the current value. At least it didn't give me the zero value.
Super User
Posts: 19,772

Re: assigning a value to a variable through a condition.

When you say previous value, are you referring to the previous value as the previous row, or the previous value as the previous value in the series. 

 

Small sample datasets that show input and output goes a long way..

Contributor
Posts: 37

Re: assigning a value to a variable through a condition.

Yes. Thank you so much Reeza.
Ask a Question
Discussion stats
  • 12 replies
  • 148 views
  • 0 likes
  • 3 in conversation