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

Hi,

For table1, I only have the observation for the year 1990, 1995, 2000, 2005 for each country. I am trying to add the values for all years (1990-2005).

Patent_rights_indexCountriesyear
2.45ALGERIA1990
2.35ALGERIA1995
2.65ALGERIA2000
2.775ALGERIA2005
0ANGOLA1990
0.875ANGOLA1995
1.075ANGOLA2000
1.2ANGOLA2005

By using the first country Algeria as an example, I expect to use the observation of the existence year (i.e., 1990,1995, 2000, 2005) for four adjacent years. For example, the observation for the year 1990 is used for the year 1991, 1992; the year 1993, 1994, 1996 and 1997 are set equal to the observation for the year 1995. they are expected to look like below,

Patent_rights_indexCountriesyear
2.45ALGERIA1990
2.45ALGERIA1991
2.45ALGERIA1992
2.35ALGERIA1993
2.35ALGERIA1994
2.35ALGERIA1995
2.35ALGERIA1996
2.35ALGERIA1997
2.65ALGERIA1998
2.65ALGERIA1999
2.65ALGERIA2000
2.65ALGERIA2001
2.65ALGERIA2002
2.775ALGERIA2003
2.775ALGERIA2004
2.775ALGERIA2005

 

Could you please give me some suggestion about this?

data table1;
	infile cards dsd  dlm=",";
	input
	Patent_rights_index	:8.
	Countries :$50.
	year :8.
   ;
	cards;
2.45,ALGERIA,1990
2.35,ALGERIA,1995
2.65,ALGERIA,2000
2.775,ALGERIA,2005
0,ANGOLA,1990
0.875,ANGOLA,1995
1.075,ANGOLA,2000
1.2,ANGOLA,2005
;;;;
run;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
andreas_lds
Jade | Level 19

The following code assumes that the data is at least grouped by "Countries", i have removed fancy unicode-chars from the datastep you have posted.

data table1;
   infile cards dsd dlm=",";
   input
   Patent_rights_index :8.
   Countries :$50.
   year :8.
   ;
   cards;
2.45,ALGERIA,1990
2.35,ALGERIA,1995
2.65,ALGERIA,2000
2.775,ALGERIA,2005
2.45,BELGIUM,1990
2.05,BELGIUM,1995
2.95,BELGIUM,2000
1.44,BELGIUM,2005
0,ANGOLA,1990
0.875,ANGOLA,1995
1.075,ANGOLA,2000
1.2,ANGOLA,2005
;;;;
run;

data want;
   set table1;
   by Countries notsorted;
      
   if first.Countries then do;
      output;
      do year = year + 1 to year + 2;
         output;
      end;
   end;
   else do;
      do year = year - 2 to year + ifn(last.Countries, 0, 2);
         output;
      end;
   end;
run;

View solution in original post

1 REPLY 1
andreas_lds
Jade | Level 19

The following code assumes that the data is at least grouped by "Countries", i have removed fancy unicode-chars from the datastep you have posted.

data table1;
   infile cards dsd dlm=",";
   input
   Patent_rights_index :8.
   Countries :$50.
   year :8.
   ;
   cards;
2.45,ALGERIA,1990
2.35,ALGERIA,1995
2.65,ALGERIA,2000
2.775,ALGERIA,2005
2.45,BELGIUM,1990
2.05,BELGIUM,1995
2.95,BELGIUM,2000
1.44,BELGIUM,2005
0,ANGOLA,1990
0.875,ANGOLA,1995
1.075,ANGOLA,2000
1.2,ANGOLA,2005
;;;;
run;

data want;
   set table1;
   by Countries notsorted;
      
   if first.Countries then do;
      output;
      do year = year + 1 to year + 2;
         output;
      end;
   end;
   else do;
      do year = year - 2 to year + ifn(last.Countries, 0, 2);
         output;
      end;
   end;
run;

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
  • 1 reply
  • 306 views
  • 0 likes
  • 2 in conversation