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

Hi, I have searched the forum for help for my problem, and I think i have an idea how to solve it, but I need some more help. I would really appreciate if someone out there could help me. I am new to SAS EG and have limited SAS programming skills. I do however have a good understanding of general code.

My data set is structured as follows:

Occurrence ID (unique)     Person ID     Date     Calculated column 1

x1                                   y1                   n1    

x2                                   y2                   n2    

x3                                   y1                   n3    

x4                                   y1                   n4    



I want to populate the calculated column according to how many days there are between two occurrences for a unique person.

For example:

For person ID = y1. Days between occurrence x3 and x1 = n3 - n1. The value will thereafter either set a 1, 2, 3 or 4 in the calculated column according to how many days there are.


My idea is to transpose the data set as follows:


Person ID (unique)     x1     x2     x3       x4    Calculated column x1     Calculated column x2 ...

y1                             n1     n2                n4       

y2                                              n3      

                                             


I can thereafter calculate by comparing columns x1,...xn for each person ID.


After this step, I need to transpose the data set back in order to have a unique calculated column values for each Occurrence ID.


Is this a good approach or do any of you have a better idea how I might attack this problem?


Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
data_null__
Jade | Level 19

I don't know that you need to transpose but you will need to sort.

data person;
   input oid person:$2.  date:date9.;
  
format date date9.;
  
cards;
1 y1 27FEB2015
2 y2 27FEB2015
3 y1 01MAR2015
4 y1 05MAR2015
;;;;
   run;
proc sort data=person;
   by person date;
   run;
data persion2;
   set person;
   by person;
   dif = dif(date);
  
if first.person then call missing(dif);
   run;
proc print;
  
run;

3-2-2015 6-52-51 AM.png

View solution in original post

2 REPLIES 2
data_null__
Jade | Level 19

I don't know that you need to transpose but you will need to sort.

data person;
   input oid person:$2.  date:date9.;
  
format date date9.;
  
cards;
1 y1 27FEB2015
2 y2 27FEB2015
3 y1 01MAR2015
4 y1 05MAR2015
;;;;
   run;
proc sort data=person;
   by person date;
   run;
data persion2;
   set person;
   by person;
   dif = dif(date);
  
if first.person then call missing(dif);
   run;
proc print;
  
run;

3-2-2015 6-52-51 AM.png
SASGT
Calcite | Level 5

Thank you!

Much easier and exactly what I want to achieve.

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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
  • 2 replies
  • 2705 views
  • 0 likes
  • 2 in conversation