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

Hi folks!

 

I am trying to manipulate data to split into multiple rows, one per month/year, and I'm very new to writing arrays in SAS. I've posted an example of my data below. Currently, it's organized as one row per "visit." There is a unique id for each person, an "in" date (the day their visit began), an "out" date (the day they left), and a special indicator ("ind") which denotes whether they had another visit within the previous 2 years.

 

I need to do two things: (1) split the records into one row per person, per visit, per month, and (2) retain the "ind" for each record.

 

One extra quirk: If their visit "out" date is on the first day of any given month, I do not want a row for that month (if they left on the first day of the month then their stay did not span into that month).

 

Hope I explained well! I tried my best 🙂

 

What my data looks like now:

Obsidinoutind
1111110/18/201010/28/2010.
211111/17/20111/24/20111
322227/9/201110/9/2011.
433334/7/20104/19/2010.
533331/10/20112/24/20111
644448/31/20099/17/2009.
755553/31/20095/1/2009.

 

What I'm trying to do:

Obsidmonthyearind
11111102010.
21111120111
3222272011.
4222282011.
5222292011.
62222102011.
7333342010.
83333120111
93333220111
10444482009.
11444492009.
12555532009.
13555542009.

(note there is no record for 5555 month=5; because they left on the 1st of the month)

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Assuming In and Out are SAS date values and not strings I think this works:


data want;
   set have;
   date=in;
   if in lt out then do while (date lt out) ;
      month= month(date);
      year = year (date);
      output;
      date = intnx('month',date,1);
   end;
else put "WARNING: Out is before In for ID: " ID; drop date in out; run;

 

View solution in original post

2 REPLIES 2
ballardw
Super User

Assuming In and Out are SAS date values and not strings I think this works:


data want;
   set have;
   date=in;
   if in lt out then do while (date lt out) ;
      month= month(date);
      year = year (date);
      output;
      date = intnx('month',date,1);
   end;
else put "WARNING: Out is before In for ID: " ID; drop date in out; run;

 

he_ko
Fluorite | Level 6

Yaaaaaaaaaaaay it worked. Thank you! Woman Very Happy

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is ANOVA?

ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 4565 views
  • 1 like
  • 2 in conversation