BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sagulolo
Quartz | Level 8

Hi All,

 

I have a code below:-

 

 

data testing;
  infile datalines dlm='|';
  input  date $ acctno $ due $ pic $;
;
datalines;
20170801 | 12345   | 0000000 | UserA
20170802 | 12345   | 0000000 | UserA
20170803 | 12345   | 0000000 | UserX
20170804 | 12345   | 0000001 | UserA
20170805 | 12345   | 0000002 | UserC
20170806 | 12345   | 0000003 | UserC
20170807 | 12345   | 0000004 | UserA
20170802 | 22222   | 0000000 | UserX
20170803 | 22222   | 0000000 | UserB
20170804 | 22222   | 0000001 | UserA
20170805 | 22222   | 0000002 | UserA
;

proc sort data=testing;
	by acctno date;
run;

data want;
	set testing;
	by acctno date;
	retain tag;
	if first.acctno then 
		do;
			if pic NE 'UserX' then tag = 'Normal';
			else tag = 'UserX';
		end;
	else 
		tag = lag(tag);
run;

and outcome as below:-

 

 
dateacctnoduepictag
20170801123450UserANormal
20170802123450UserA 
20170803123450UserXNormal
20170804123451UserA 
20170805123452UserCNormal
20170806123453UserC 
20170807123454UserANormal
20170802222220UserXUserX
20170803222220UserB 
20170804222221UserAUserX
20170805222222UserA 

 

How can i get below output? logic is once acc's pic is UserX, then later the rest will become UserX.

 

dateacctnoduepictag
20170801123450UserANormal
20170802123450UserANormal
20170803123450UserXUserX
20170804123451UserAUserX
20170805123452UserCUserX
20170806123453UserCUserX
20170807123454UserAUserX
20170802222220UserXUserX
20170803222220UserBUserX
20170804222221UserAUserX
20170805222222UserAUserX

 

Thank in advance

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

If you study the documentation closely, you'll see that LAG does not retrieve the value from the previous observation.  Instead, it retrieves the value from the last time that the LAG function executed.  It's tricky.

 

The easiest fix would be to change the ELSE statement:

 

else if pic='UserX' then tag='UserX';

View solution in original post

4 REPLIES 4
Astounding
PROC Star

If you study the documentation closely, you'll see that LAG does not retrieve the value from the previous observation.  Instead, it retrieves the value from the last time that the LAG function executed.  It's tricky.

 

The easiest fix would be to change the ELSE statement:

 

else if pic='UserX' then tag='UserX';

sagulolo
Quartz | Level 8

Astounding, thank you so much.

 

Would you mind to tell me how it work, by study the else code your give, Im confuse how SAS run the data already.

 

Best Regards

Astounding
PROC Star

One of the keys to how this works is the RETAIN statement.  Once TAG is set, it doesn't need to be re-set on each observation.  RETAIN just holds on to the value that was already there.

 

So on the first observation for each account, set the initial value for TAG.  Then just let it sit there until "UserX" is found.  For "UserX", change TAG.  Then as before ... let the current value (which is now "UserX") just sit there.

sagulolo
Quartz | Level 8

Thank you, Astounding.

 

Now I understand how it work. Thank for your time and guidance.

Really help me a lot

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
  • 4 replies
  • 840 views
  • 1 like
  • 2 in conversation