BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
MC10
Calcite | Level 5

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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.

View solution in original post

2 REPLIES 2
Kurt_Bremser
Super User

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.

MC10
Calcite | Level 5
It works perfectly! Thank you very much for the help.

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 812 views
  • 0 likes
  • 2 in conversation