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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 690 views
  • 0 likes
  • 2 in conversation