BookmarkSubscribeRSS Feed
jjadall1
Quartz | Level 8

Hello,

I currently have a dataset with 1 observation per firm in various years.  For example, I have a firm with a 1998 observation.  Is there a way to code it where I can get lines for 1995, 1996, 1997, 1999, 2000, and 2001 observations for the same firm (3 years before and 3 years after)?  That way, instead of having 1 observation per firm, I will have 7.  Finally, I would like to delete the original observation (1998 in my example).

 

Thanks so much!

Jadallah

8 REPLIES 8
pearsoninst
Pyrite | Level 9
If you give an example data it will help
jjadall1
Quartz | Level 8

The dataset I'm working with is attached.

 

Thanks so much!

Jadallah

pearsoninst
Pyrite | Level 9
This code is working , but i think you get better soluation in this forum . 

data newdata; Input Id$ Year; datalines; A 1998 ; run; Data want1; set newdata; Do i = 1 to 3; Year1 = Year+(i); Output; End; drop i year; run; Data want2; set newdata; Do i = 1 to 3; Year2 = Year-(i); Output; End; drop i year; run; Proc SQL; Select* from want1 union All Select* from want2; quit;

 

Patrick
Opal | Level 21

@pearsoninst

An simpler coding variant for what you're proposing is:

data have;
  input Id$ Year;
  datalines;
A 1998
;
run;
data want(drop=_y);
  set have(rename=(year=_y));
  do year= _y-3 to _y-1 , _y+1 to _y+3 ;
    output;
  end;
run;
 

 

pearsoninst
Pyrite | Level 9
Cool one Patrick, I was little lost with the below code
do year= _y-3 to _y-1 , _y+1 to _y+3 ;
Patrick
Opal | Level 21

The do loop allows for quite a few different variations of syntax. 

Have a look at Example1: 

https://support.sas.com/documentation/cdl/en/lestmtsref/68024/HTML/default/viewer.htm#p1cydk5fq0u4bf...

 

Patrick
Opal | Level 21

What should happen if a firm has multiple entries in your source dataset?

Can you please show us your desired result for _cik=0001046649

Capture.PNG

 

Not my area of expertise but should you intend to create such data as preparation for time series then look into Proc Expand.

jjadall1
Quartz | Level 8

Thanks for the help - God bless you and your family!  The reason there are multiple entries of the same firm is because I have a treatment group and a control group; with the control group, I did matching with replacement.  It would be great to know how to do matching without replacement for the future.

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
  • 8 replies
  • 2203 views
  • 2 likes
  • 3 in conversation