BookmarkSubscribeRSS Feed
fordcr2
Obsidian | Level 7

I have survey data for participants over a year long study, but not everyone filled out a survey each month as expected. There are a lot of people in this dataset so I want to do this systematically.  Some people have surveys in multiple years ex) their first survey may be in November 2021 but they have surveys through 2022. 

 

Survey_m_yq1q2q3
June 2021120
August 2021342
September 2021403

 

I want the data to look like this:

Survey_m_ystudy_timeq1q2q3
June 20211120
July 20212   
August 20213342
September 20214403

 

What is the easiest way to fill in the missing month/year dates so that I can assign a study_time variable for each person that has 12 observations?

3 REPLIES 3
PaigeMiller
Diamond | Level 26

Work with actual numeric SAS dates, instead of text strings like "June 2021". Numeric dates will sort properly and makes it much easier for you to detect gaps.

--
Paige Miller
fordcr2
Obsidian | Level 7

I have the dates and can use those, but how would I fill in the missing months still? 

PaigeMiller
Diamond | Level 26

Example assuming you have numeric dates representing the 1st day of each month, fake data from Jan 2020 to Jan 2022

 

data have;
    date=mdy(1,1,2020);
    q1=1;
    output;
    date=mdy(4,1,2020);
    q1=4;
    output;
    format date monyy7.;
run;
data all_months;
    start_date=mdy(1,1,2020);
    do study_time = 0 to 24;
        date=intnx('month',start_date,count,'b');
        output;
    end;
    format date monyy7.;
    drop start_date;
run;
data want;
    merge have all_months;
    by date;
run;

 

 

--
Paige Miller
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
  • 3 replies
  • 623 views
  • 0 likes
  • 2 in conversation