Hello: I know we have previously answered that in different versions... My challenge is how to measure changes over time when we include a string(categorical) variable. The goal is how many times a subject changes program ('pgm') over time (terms). Some of the subjects continue for three terms other stopped at two terms.
DATA example; input subject term pgm ~ $1.; datalines; 1 1 a 1 2 b 2 1 a 2 2 b 2 3 c 3 1 a 3 2 c run; proc transpose data=example out=trans; by subject; id term; var pgm; run;
I am not sure how to continue from here...to reach my goal...
Thank you
@triunk wrote:
pgmchange is a binary variable with values yes (1) or no(0) if the subject changed program
and timeschange is how many times the subjects changed program...
I am amazed with quick response.
Please forgive me for my sloppy request. Your feedback helped me clarify.
Thank you all for your quick response.
data want; set example; by subject; retain terms timeschange ; lpgm=lag(pgm); if first.subject then do; timeschange=0; terms=1; end; else do; terms+1; timeschange= timeschange+(lpgm ne pgm); end; pgmchange = (timeschage>0); if last.subject; drop term lpgm pgm; run;
that posting is hard to read, It is like a run on sentence or an old time NY stock exchange.
Could you please repost your question in a normal viewable form.
Thank you both for clean version and quick feedback -
The example suggest does not fit my case (at least I think...) here is an output that I was envisioning...
subject Terms Pgmchange Timeschange
1 2 1 1
2 3 1 2
3 2 1 1
Does this make better sense of an output?
Here's an example then, just count the number of times the PGM is first using FIRST/LAST.
data want;
set have;
by subject term pgm NOTSORTED; *since you can't sort the data the notsorted option will need to be used;
RETAIN pgmChange;
*set at start of each Subject;
if first.subject then pgmChange=-1;
*increment for each change;
if first.pgm then pgmChange+1;
if last.subject then output;
run;
@triunk wrote:
Thank you both for clean version and quick feedback -
The example suggest does not fit my case (at least I think...) here is an output that I was envisioning...
subject Terms Pgmchange Timeschange
1 2 1 1
2 3 1 2
3 2 1 1
Does this make better sense of an output?
@triunk wrote:
Thank you both for clean version and quick feedback -
The example suggest does not fit my case (at least I think...) here is an output that I was envisioning...
subject Terms Pgmchange Timeschange
1 2 1 1
2 3 1 2
3 2 1 1
Does this make better sense of an output?
You will have to tell us what the heck pgmchange is based on.
You might also include an example of the output for subject with only one observation and maybe another one or two subjects where the pgm changes back to a previous value.
I would start with something like:
data want; set example; by subject; retain terms timeschange ; lpgm=lag(pgm); if first.subject then do; timeschange=0; terms=1; end; else do; terms+1; timeschange= timeschange+(lpgm ne pgm); end; if last.subject; drop term lpgm pgm; run;
but without a definition of Pgmchange that's as far as I can go.
pgmchange is a binary variable with values yes (1) or no(0) if the subject changed program
and timeschange is how many times the subjects changed program...
I am amazed with quick response.
Please forgive me for my sloppy request. Your feedback helped me clarify.
Thank you all for your quick response.
@triunk wrote:
pgmchange is a binary variable with values yes (1) or no(0) if the subject changed program
and timeschange is how many times the subjects changed program...
I am amazed with quick response.
Please forgive me for my sloppy request. Your feedback helped me clarify.
Thank you all for your quick response.
data want; set example; by subject; retain terms timeschange ; lpgm=lag(pgm); if first.subject then do; timeschange=0; terms=1; end; else do; terms+1; timeschange= timeschange+(lpgm ne pgm); end; pgmchange = (timeschage>0); if last.subject; drop term lpgm pgm; run;
Would proc summary solve your problem.
this summary sample is on term
proc summary data=example;
by subject;
id term;
var term;
output out=trans (drop= _TYPE_ _FREQ_ _ORGA ) sum()= mean(term)=;
run;
The SAS System
Obs subject term
1 1 2
2 2 3
3 3 2
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.