BookmarkSubscribeRSS Feed
BonnaryW
Obsidian | Level 7

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.

12 REPLIES 12
art297
Opal | Level 21

You can't use first.pn without including a 

by pn;

statement

 

Art, CEO, AnalystFinder.com

 

Reeza
Super User

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;

 

art297
Opal | Level 21

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

 

BonnaryW
Obsidian | Level 7

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. 

art297
Opal | Level 21

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

 

Art, CEO, AnalystFinder.com

 

BonnaryW
Obsidian | Level 7
I am sorry Art, The date is SAS dateformat as date9. Thank you.
art297
Opal | Level 21

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

 

BonnaryW
Obsidian | Level 7
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.

Reeza
Super User

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

BonnaryW
Obsidian | Level 7
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.
Reeza
Super User

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..

BonnaryW
Obsidian | Level 7
Yes. Thank you so much Reeza.

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
  • 12 replies
  • 925 views
  • 0 likes
  • 3 in conversation