BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
abdulla
Pyrite | Level 9
gvkeyfyearindsaleGrowth
100419801214.
103219801214.
103519801214.
1055198113321.551402
1070198113321.551402
1086198113321.551402
1101198113321.551402
1117198214451.340361
1132198214451.340361
1148198214451.340361
100519802325.
102219802325.
102719802325.
1040198123561.095385
1051198123561.095385
1062198123561.095385
1073198123321.095385
1084198222660.801205
1095198222660.801205
1106198222660.801205


I want to calculate industry sales growth. The last column is my expected result. sale/lag(sale). But I want it the way I have shown on the table. Can anyone help? 

1 ACCEPTED SOLUTION

Accepted Solutions
mklangley
Lapis Lazuli | Level 10

@abdulla   I just made up an example, which might capture what you brought up. Does this help?
(Note: I am assuming the input data is sorted by ind, then fyear.)

data have;
    input gvkey fyear ind sale;
    datalines;
    1004 1980 1 214
    1032 1980 1 214
    1035 1980 1 214
    1055 1981 1 332
    1070 1981 1 332
    1086 1981 1 332
    1101 1981 1 332
    1117 1982 1 445
    1132 1982 1 445
    1148 1982 1 445
    1234 1980 2 100
    2345 1980 2 100
    3456 1981 2 115
    4567 1981 2 115
    5678 1982 2 115
    6789 1982 2 115
    4321 1983 2 136
    5432 1983 2 136
    6543 1984 2 175
    7654 1984 2 175
    ;
run;

data want;
    set have;
    retain growth 0;
    by ind fyear;
    if first.fyear
        then growth = sale / lag(sale);
    if first.ind then growth = .;
run;

mklangley_0-1602104353209.png

 

View solution in original post

4 REPLIES 4
mklangley
Lapis Lazuli | Level 10

@abdulla   Is this what you're looking for?

data have;
    input gvkey fyear ind sale;
    datalines;
    1004 1980 1 214
    1032 1980 1 214
    1035 1980 1 214
    1055 1981 1 332
    1070 1981 1 332
    1086 1981 1 332
    1101 1981 1 332
    1117 1982 1 445
    1132 1982 1 445
    1148 1982 1 445
    ;
run;

data want;
    set have;
    retain growth 0;
    by sale;
    if first.sale
        then growth = sale / lag(sale);
run;

Result:

mklangley_0-1602103197215.png

abdulla
Pyrite | Level 9
Yes. But how about if I have more industries and sale is the same in any of the industries for two years? I have 48 industries and year is between 10980 and 1990. So, instead of first.sale I think I have to use first.fyear
mklangley
Lapis Lazuli | Level 10

Yes, that would make sense, although it depends on what you are wanting to calculate: the growth since the last year, or the growth since the last sale amount, and whether it should be broken down by industry, etc.  To figure those out, you could use some broader data (or introduce some more of those scenarios input your input dataset).

mklangley
Lapis Lazuli | Level 10

@abdulla   I just made up an example, which might capture what you brought up. Does this help?
(Note: I am assuming the input data is sorted by ind, then fyear.)

data have;
    input gvkey fyear ind sale;
    datalines;
    1004 1980 1 214
    1032 1980 1 214
    1035 1980 1 214
    1055 1981 1 332
    1070 1981 1 332
    1086 1981 1 332
    1101 1981 1 332
    1117 1982 1 445
    1132 1982 1 445
    1148 1982 1 445
    1234 1980 2 100
    2345 1980 2 100
    3456 1981 2 115
    4567 1981 2 115
    5678 1982 2 115
    6789 1982 2 115
    4321 1983 2 136
    5432 1983 2 136
    6543 1984 2 175
    7654 1984 2 175
    ;
run;

data want;
    set have;
    retain growth 0;
    by ind fyear;
    if first.fyear
        then growth = sale / lag(sale);
    if first.ind then growth = .;
run;

mklangley_0-1602104353209.png

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

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
  • 4 replies
  • 667 views
  • 2 likes
  • 2 in conversation