Help using Base SAS procedures

Simple Question on extracting rows

Reply
N/A
Posts: 0

Simple Question on extracting rows

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! Smiley Happy Message was edited by: am
N/A
Posts: 0

Re: Simple Question on extracting rows

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;
N/A
Posts: 0

Re: Simple Question on extracting rows

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!
Contributor
Posts: 73

Re: Simple Question on extracting rows

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.
Ask a Question
Discussion stats
  • 3 replies
  • 109 views
  • 0 likes
  • 2 in conversation