BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.

dear all

my data is in the following format 

company_nameyearCA
20 Microns Ltd.2011958.9
20 Microns Ltd.20121185.3
20 Microns Ltd.20131097.5
20 Microns Ltd.20141341.1
20 Microns Ltd.20151505.3
20 Microns Ltd.20161631.1
20 Microns Ltd.20171557.9
20 Microns Ltd.20181679.5
20 Microns Ltd.20191893.7
20 Microns Ltd.20201957.2
20 Microns Ltd.20212040.1
3I Infotech Ltd.20119043.3
3I Infotech Ltd.20126329.7
3I Infotech Ltd.20136221.6
3I Infotech Ltd.20146976.8
3I Infotech Ltd.20156608.5
3I Infotech Ltd.20167162.7
3I Infotech Ltd.20177443.1
3I Infotech Ltd.20187101.5
3I Infotech Ltd.20197944.2
3I Infotech Ltd.20208165.2
3I Infotech Ltd.202111286.2
3M India Ltd.20114652.1
3M India Ltd.20125684.4
3M India Ltd.20138996.1
3M India Ltd.20149574.9
3M India Ltd.20157634.6
3M India Ltd.201610677.2
3M India Ltd.201715550.9
3M India Ltd.201817775.3
3M India Ltd.201915342.9
3M India Ltd.202017414.6
3M India Ltd.202119899.4
3P Land Holdings Ltd.2011465.9
3P Land Holdings Ltd.2012518
3P Land Holdings Ltd.2013502.6
3P Land Holdings Ltd.2014483.2
3P Land Holdings Ltd.20169.1
3P Land Holdings Ltd.20173.1
3P Land Holdings Ltd.20182.7
3Rd Rock Multimedia Ltd.2014110.7
3Rd Rock Multimedia Ltd.201996.2
52 Weeks Entertainment Ltd.2018388.4
52 Weeks Entertainment Ltd.2019295.9
63 Moons Technologies Ltd.201110732.7
63 Moons Technologies Ltd.201214470.4
63 Moons Technologies Ltd.201316083.7
63 Moons Technologies Ltd.201413040.1
63 Moons Technologies Ltd.201517269
63 Moons Technologies Ltd.20167948.6
63 Moons Technologies Ltd.20177197.1
63 Moons Technologies Ltd.201813677.4
63 Moons Technologies Ltd.201913101.7
63 Moons Technologies Ltd.202013394.7
63 Moons Technologies Ltd.202113280.4

 

it is a unbalanced panel data of nearly 3,000 companies, from 2011 to 2021.

 i have to create one year lag value of the variable  "CA".

my output should be in the following format

 

company_nameyearCACA Lag
20 Microns Ltd.2011958.9 
20 Microns Ltd.20121185.3958.9
20 Microns Ltd.20131097.51185.3
20 Microns Ltd.20141341.11097.5
20 Microns Ltd.20151505.31341.1
20 Microns Ltd.20161631.11505.3
20 Microns Ltd.20171557.91631.1
20 Microns Ltd.20181679.51557.9
20 Microns Ltd.20191893.71679.5
20 Microns Ltd.20201957.21893.7
20 Microns Ltd.20212040.11957.2
3I Infotech Ltd.20119043.3 
3I Infotech Ltd.20126329.79043.3
3I Infotech Ltd.20136221.66329.7
3I Infotech Ltd.20146976.86221.6
3I Infotech Ltd.20156608.56976.8
3I Infotech Ltd.20167162.76608.5
3I Infotech Ltd.20177443.17162.7
3I Infotech Ltd.20187101.57443.1
3I Infotech Ltd.20197944.27101.5
3I Infotech Ltd.20208165.27944.2
3I Infotech Ltd.202111286.28165.2
3M India Ltd.20114652.1 
3M India Ltd.20125684.44652.1
3M India Ltd.20138996.15684.4
3M India Ltd.20149574.98996.1
3M India Ltd.20157634.69574.9
3M India Ltd.201610677.27634.6
3M India Ltd.201715550.910677.2
3M India Ltd.201817775.315550.9
3M India Ltd.201915342.917775.3
3M India Ltd.202017414.615342.9
3M India Ltd.202119899.4

17414.6

 

 

please suggest me a SAS code  to get the output in the above mentioned format

 

thanking you in advance 

1 ACCEPTED SOLUTION

Accepted Solutions
Adriaan_Gouws
Obsidian | Level 7

Good day

 

Kindly give the below a try. I've created dummy data to get the point across. I've also done it in smaller steps, to show you how to get to the final result. Whenever you need to lag something by a group, that group needs to be sorted, that's why there's a sort step. This can also be done in PROC SQL if you are more familiar with SQL. 

DATA HAVE;
	INPUT GROUP $ NUMBER;
	DATALINES;
A 1
A 2
A 3
A 4
A 5
B 1
B 2
B 3
B 4
B 5
	;
RUN;

PROC SORT DATA=HAVE OUT=SORT;
	BY GROUP;
RUN;

DATA WANT;
	SET SORT;
	BY GROUP;
	LAG_NUMBER = LAG1(NUMBER);

	IF FIRST.GROUP THEN
		LAG_NUMBER_CLEAN = .;
	ELSE LAG_NUMBER_CLEAN = LAG_NUMBER;
RUN;

View solution in original post

4 REPLIES 4
srikanthyadav44
Quartz | Level 8

could you please provide one illustration of the SAS CODE for this purpose

Kurt_Bremser
Super User

It's simple; try it on your own, and let's see what you come up with.

 

The SAS communities are meant to assist people in their SAS life, not for doing other's work. If you want someone else to do your work for you, hire a consultant.

Adriaan_Gouws
Obsidian | Level 7

Good day

 

Kindly give the below a try. I've created dummy data to get the point across. I've also done it in smaller steps, to show you how to get to the final result. Whenever you need to lag something by a group, that group needs to be sorted, that's why there's a sort step. This can also be done in PROC SQL if you are more familiar with SQL. 

DATA HAVE;
	INPUT GROUP $ NUMBER;
	DATALINES;
A 1
A 2
A 3
A 4
A 5
B 1
B 2
B 3
B 4
B 5
	;
RUN;

PROC SORT DATA=HAVE OUT=SORT;
	BY GROUP;
RUN;

DATA WANT;
	SET SORT;
	BY GROUP;
	LAG_NUMBER = LAG1(NUMBER);

	IF FIRST.GROUP THEN
		LAG_NUMBER_CLEAN = .;
	ELSE LAG_NUMBER_CLEAN = LAG_NUMBER;
RUN;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 497 views
  • 0 likes
  • 3 in conversation