Hi all,
I have a coding problem. Basically, I want to add more rows and change Data A to Data B.
That is, for each code A and B, I want to have observations for 2004, 2005, ..., 2011.
In Code=B, new rows before the first year in Data A should include the first year's value, while new rows after the last year in Data A should include the first year's value.
Could you help me how to make a code for the transformation from Data A to Data B?
Thank you!
(Data A)
Code value year
A | 10 | 2005 |
B | 8 | 2005 |
B | 8 | 2006 |
B | 9 | 2007 |
B | 10 | 2008 |
B | 10 | 2009 |
B | 11 | 2010 |
(Data B)
Code value year
A | 10 | 2004 |
A | 10 | 2005 |
A | 10 | 2006 |
A | 10 | 2007 |
A | 10 | 2008 |
A | 10 | 2009 |
A | 10 | 2010 |
A | 10 | 2011 |
B | 8 | 2004 |
B | 8 | 2005 |
B | 8 | 2006 |
B | 9 | 2007 |
B | 10 | 2008 |
B | 10 | 2009 |
B | 11 | 2010 |
B | 11 | 2011 |
Do this:
data want;
set have;
by code;
if first.code then do year = 2004 to year - 1;
output;
end;
output;
if last.code then do year = year + 1 to 2011;
output;
end;
run;
Untested; for tested code, supply example data in a data step with datalines.
Do this:
data want;
set have;
by code;
if first.code then do year = 2004 to year - 1;
output;
end;
output;
if last.code then do year = year + 1 to 2011;
output;
end;
run;
Untested; for tested code, supply example data in a data step with datalines.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.