BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Hi,

I was wondering if someone can help me with this simple problem:

I have a long database that resembles the format below:
Col1 Col2 Col3
1.......x1........0
2.......x2........0
3.......x3........0
4.......x4........0
5.......x5........1
6.......x6........0
7.......x7........0
8.......x8........0

I need to extract 2 rows before and after the "1" in my third column.

I was thinking that I can ask SAS to do the following: When Col3 = 1 it should copy the observation from Col1 (which is 5) and create Col4 with the following variable:
Col4 = Col1 - "5" . i.e:

Col1 Col2 Col3 Col4
1.......x1........0......-4
2.......x2........0......-3
3.......x3........0......-2
4.......x4........0......-1
5.......x5........1.......0
6.......x6........0.......1
7.......x7........0.......2
8.......x8........0.......3
However I am not sure how to store "5" to use it later. Maybe i can even create another column with all "5"s.

Then I can delete all observations greater and less than +2 and -2 respectively.

So the final dataset would look like this:

Col1 Col2 Col3 Col4

3.......x3........0......-2
4.......x4........0......-1
5.......x5........1.......0
6.......x6........0.......1
7.......x7........0.......2


I need to repeat the procedure above several times.

Thank you in advance for all your comments and any alternative suggestions! :) Message was edited by: am
3 REPLIES 3
deleted_user
Not applicable
Hi,
I think the following code is helpful to store '5' permanantly;
Proc sql;
select(col1) into:a from libref.datasetname where col1=5;
quit;
deleted_user
Not applicable
Thank you.

I found another way to do it.

Save the constant that i need (in this case =5) as a macro using the symput function.

Thanks for your response again!
JohnH
Fluorite | Level 6
I think you could create three new variables, newcol1, newcol2, newcol3. Then, use a retain statement on newcol1, newcol2, newcol3. Then, stuff the values of col1 - col3 into the newcol1-newcol3. They will be retained so that the next record read will have the previous values in newcol1 - newcol3. You can then do what you want to output whatever variables you want, based on the values of the previous record.

CFC_SAS_Communities_400x225.jpg

Call for content now open!

It's your turn to help shape SAS Innovate 2027. Share your expertise and inspire the SAS community.

Submit your proposal →

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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