BookmarkSubscribeRSS Feed
mandyosu01
Obsidian | Level 7

Hello SAS users,

 

Thank you so much in advance for reading the question. 

 

I have the following sample data and code. These are the data steps I needed:

1. From the first firm, Firm 1, randomly match with another firm, by Group

2. Create a new variable, Year 2 Profit. Use the matched firm's Rate as a growth rate to multiply Firm 1's Year 1 Profit to calculate Firm 1 's Year 2 Profit. Year 2 Profit= Rate * Year 1 Profit 

3. Use the new variable, Year 2 Profit, to rematch with a random firm, by Group.

4. Create a new variable, Year 3 Profit. Use the newly matched firm's Rate as a growth rate to multiply Year 2 Profit. Year 3 Profit = Rate * Year 2 Profit 

 

What I look for:

Get all Year 4,5,6,7 Year Profit... for Firm 1.

Get all Year 2,3,4,5,6,7 Year Profit... for all other firms.

Some firms have more than 7 years of data, and some firms have less than 7 years of data. so some may have Year 8 Profit, Year 9 Profit...

 

Firm 

Year

Rate 

Group 

Year 1 Profit

Year 2 Profit

Year 3 Profit

Year 4 Profit

Year 5 Profit

Year 6 Profit 

Year 7 Profit 

A

2010

0.1

1

100

 

 

 

 

 

 

A

2001

0.15

2

90

 

 

 

 

 

 

B

1998

0.01

1

101

 

 

 

 

 

 

B

1995

0.2

1

95

 

 

 

 

 

 

B

1991

0.02

2

98

 

 

 

 

 

 

C

2010

0.3

2

103

 

 

 

 

 

 

C

2020

0.05

2

110

 

 

 

 

 

 

C

2009

0.1

3

90

 

 

 

 

 

 

 


I have the select code, but dont know how to repeat the process.


proc surveyselect data=have  sampsize=1 out=mySamples;
strata group;
run;

 

Data a1, set mySamples;

Year 2 Profit  = Year 1 Profit * Rate;

Run;

 

proc surveyselect data=a1  sampsize=1 out=mySamples2;
strata group;
run;

 

Data a2, set mySamples2;

Year 3 Profit  = Year 2 Profit * Rate;

Run;

 

Thank you so much in advance!

 

 

4 REPLIES 4
data_null__
Jade | Level 19

Use REPS= on the PROC statement.

mandyosu01
Obsidian | Level 7

Thanks. I dont think the repts = works here since I have a calculation between each random draw. 

Reeza
Super User

If you have a program that works, see the below tutorial on converting it to a macro. 
But know that I do agree with @data_null__ suggestion of adding REPS and then using BY group processing instead. It's hard to follow exactly what's going on, I don't see how you're matching anything for example. 

Tutorial on converting a working program to a macro

This method is pretty robust and helps prevent errors and makes it much easier to debug your code. Obviously biased, because I wrote it 🙂 https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md

Examples of common macro usage

https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Ap...

mandyosu01
Obsidian | Level 7

Thank you for the information. I will look into it. I am matching firms by group. As long as they are in the same group, they could be matched. Can you help me with the Macro since I am very new to Macro. Thanks.

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
  • 4 replies
  • 1228 views
  • 0 likes
  • 3 in conversation