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:-
date | acctno | due | pic | tag |
20170801 | 12345 | 0 | UserA | Normal |
20170802 | 12345 | 0 | UserA | |
20170803 | 12345 | 0 | UserX | Normal |
20170804 | 12345 | 1 | UserA | |
20170805 | 12345 | 2 | UserC | Normal |
20170806 | 12345 | 3 | UserC | |
20170807 | 12345 | 4 | UserA | Normal |
20170802 | 22222 | 0 | UserX | UserX |
20170803 | 22222 | 0 | UserB | |
20170804 | 22222 | 1 | UserA | UserX |
20170805 | 22222 | 2 | UserA |
How can i get below output? logic is once acc's pic is UserX, then later the rest will become UserX.
date | acctno | due | pic | tag |
20170801 | 12345 | 0 | UserA | Normal |
20170802 | 12345 | 0 | UserA | Normal |
20170803 | 12345 | 0 | UserX | UserX |
20170804 | 12345 | 1 | UserA | UserX |
20170805 | 12345 | 2 | UserC | UserX |
20170806 | 12345 | 3 | UserC | UserX |
20170807 | 12345 | 4 | UserA | UserX |
20170802 | 22222 | 0 | UserX | UserX |
20170803 | 22222 | 0 | UserB | UserX |
20170804 | 22222 | 1 | UserA | UserX |
20170805 | 22222 | 2 | UserA | UserX |
Thank in advance
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';
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';
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
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.
Thank you, Astounding.
Now I understand how it work. Thank for your time and guidance.
Really help me a lot
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.