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

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

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@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;

View solution in original post

10 REPLIES 10
VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

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.

 

Reeza
Super User
Hi, I edited your question to make it legible. But it would help a lot if you showed what you want as output.
triunk
Obsidian | Level 7
Thank you so much for your generous help Reeza!
triunk
Obsidian | Level 7

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?

Reeza
Super User

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?


 

ballardw
Super User

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

 

triunk
Obsidian | Level 7

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. 

ballardw
Super User

@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;
VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 10 replies
  • 2692 views
  • 3 likes
  • 4 in conversation