Create a fourth variable from first three:
data have;
informat id $2. a b 8.;
input id $ A B ;
cards;
01 1 1
01 1 1
01 2 0
02 2 0
02 1 0
02 2 1
03 2 1
03 2 1
03 2 1
04 1 0
04 2 0
04 2 0
05 2 0
05 1 1
05 2 0
06 1 1
06 2 1
06 2 1
06 2 1
;
run;
To create
For a particular id, if A=1 firstly and subsequent A=2 for same id with B = 1 , then C=1.
But if for id A=2 firstly and subsquent A=1 for same id with B=1, then C=0. A=1 most preceed A=2 for C=1.
id A B C
01 1 1 0
01 1 1 0
01 2 0 0
02 2 0 1
02 1 0 1
02 2 1 1
03 2 1 0
03 2 1 0
03 2 1 0
04 1 0 0
04 2 0 0
04 2 0 0
05 2 0 0
05 1 1 0
05 2 0 0
06 1 1 1
06 2 1 1
06 2 1 1
06 2 1 1
Note: I've moved this thread to the Base Programming as it is not related to SAS Data Integration Studio or SAS Data Flux where are Data Management Products/tools. Additionally, I've modified your post to correct spelling and to include the data as a data step. Please try to do these on your own in the future.
Note: I've moved this thread to the Base Programming as it is not related to SAS Data Integration Studio or SAS Data Flux where are Data Management Products/tools. Additionally, I've modified your post to correct spelling and to include the data as a data step. Please try to do these on your own in the future.
Thank you!!!!
Please provide also a SAS datastep or at least a table which shows your desired output. This will help us to better understand the logic you describe so we don't spend time for a solution which doesn't return what you're after.
Please try
data want;
do until(last.id);
length codec$100.;
set have;
by id;
retain codec;
if a=1 then code='a';
else if a=2 then code='b';
if first.id then codec=code;
else codec=compress(catx('',codec,code));
if prxmatch('m/ab/oi',strip(codec)) and b=1 then c=1;
else if prxmatch('m/ba/oi',strip(codec)) and b=1 then c=0;
else c=0;
end;
do until(last.id);
set have;
by id;
output;
end;
drop code codec;
run;
Hello Jag,
Thank you. let me verify.
Best
Really like your solution @Jagadishkatam
@desireatem you marked the wrong solution correct.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.