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

Hi all SAS experts,

 

I have a code as below

   if  geogn="UNITEDS"        & yr=1989 then cvt_yr=-4;
   else 
   if  geogn="UNITEDS"        & yr=1990 then cvt_yr=-3;
   else 
   if  geogn="UNITEDS"        & yr=1991 then cvt_yr=-2;
      else 
   if  geogn="UNITEDS"        & yr=1992 then cvt_yr=-1;
      else 
   if  geogn="UNITEDS"        & yr=1993 then cvt_yr=0;
      else 
   if  geogn="UNITEDS"        & yr=1994 then cvt_yr=1;
      else 
   if  geogn="UNITEDS"        & yr=1995 then cvt_yr=2;
      else 
   if  geogn="UNITEDS"        & yr=1996 then cvt_yr=3;
      else 
   if  geogn="UNITEDS"        & yr=1997 then cvt_yr=4;
      else 
   if  geogn="UNITEDS"        & yr=1998 then cvt_yr=5;

I am wondering how to shorten the code (because I need to do the same thing for 64 countries). And the time series is from 1990 to 2020, but I just want to focus on 9 years around 1993 as above (I mean, a condition for  -4 <= cvt_yr <= 5)

Thank you in advance.

Have a good weekend,

Phil.

Thank you for your help, have a fabulous and productive day! I am a novice today, but someday when I accumulate enough knowledge, I can help others in my capacity.
1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@Phil_NZ wrote:

Hi @jimbarbour 

 

Thank you so much for your dedicated help, however, now I want to focus only on one country, and as the edited question above, could you please give me a hint to deal with? Because in your previous code, you did not restrict the value of cvt_yr.


You may need to define what you mean by focus on one country.

Easiest might be to subset the data with a where:

 

data want;
   set have;
   where    geogn="UNITEDS" and year in (1989:1998);
   cvt_yr= year-1993;
run;

Or:

one IF

 if geogn="UNITEDS" and year in (1989:1998) then cvt_yr= year-1993;

 

View solution in original post

6 REPLIES 6
jimbarbour
Meteorite | Level 14

@Phil_NZ ,

 

Hopefully by now you've seen my response to your other topic.  My response on the other topic was written to cover this topic as well:  I made the program data driven such that it really doesn't matter whether you have 7 countries or 7000 to deal with.  You just build a table listing everything out, and the program will use macros to generate the code for you.

 

If you look closely at the code, you'll see that I'm doing all the setting of the variables in just three IF statements.  I don't know how much more I can streamline your IF statements than that.

 

Jim

Phil_NZ
Barite | Level 11

Hi @jimbarbour 

 

I see your post already, thank you so much, I am trying to digest it. I will come back and hopefully you can help me to follow up my upcoming questions about the given code.

 

Many thanks and cheers,

Phil.

Thank you for your help, have a fabulous and productive day! I am a novice today, but someday when I accumulate enough knowledge, I can help others in my capacity.
Phil_NZ
Barite | Level 11

Hi @jimbarbour 

 

Thank you so much for your dedicated help, however, now I want to focus only on one country, and as the edited question above, could you please give me a hint to deal with? Because in your previous code, you did not restrict the value of cvt_yr.

Thank you for your help, have a fabulous and productive day! I am a novice today, but someday when I accumulate enough knowledge, I can help others in my capacity.
ballardw
Super User

@Phil_NZ wrote:

Hi @jimbarbour 

 

Thank you so much for your dedicated help, however, now I want to focus only on one country, and as the edited question above, could you please give me a hint to deal with? Because in your previous code, you did not restrict the value of cvt_yr.


You may need to define what you mean by focus on one country.

Easiest might be to subset the data with a where:

 

data want;
   set have;
   where    geogn="UNITEDS" and year in (1989:1998);
   cvt_yr= year-1993;
run;

Or:

one IF

 if geogn="UNITEDS" and year in (1989:1998) then cvt_yr= year-1993;

 

Ksharp
Super User
Make a format :

proc format
invalue UNITEDS
1989=-4
1990=-3
.....



..........................
if geogn="UNITEDS" then cvt_yr=input(yr, UNITEDS. );
jimbarbour
Meteorite | Level 14

the time series is from 1990 to 2020, but I just want to focus on 9 years around 1993 as above (I mean, a condition for  -4 <= cvt_yr <= 5)

Hi, @Phil_NZ ,

I guess I didn't catch that you were focusing on a particular range for cvt_yr.  What @ballardw and @Ksharp have advised is good.  You could also use the very range that you coded above (-4 <= cvt_yr <= 5), like so:

%LET	GeoGn					=	UNITEDS;
%LET	StartYr					=	1993;

DATA	gen_post_treat;
	set matching;
	yr=input(year, ?? 32.);	/*year is a character variable, so I convert to numeric variable*/

	IF	STRIP(UPCASE(GeoGn))	=	"&GeoGn";

	Cvt_Yr						=	Yr	-	&StartYr;

	IF	-4 		<= 	cvt_yr 		<= 	5;
RUN;

If you need to switch to another year and start date, then you would edit the %LET statements.  Note that because these are macro statements that you do NOT put quotes around the GeoGn value.  

 

If I code up some test data as follows:

DATA	Matching;
	LENGTH	GeoGn	$12;
	LENGTH	Year	$4;
	INPUT	GeoGn	$
			Year	$
			;
DATALINES;
UNITEDS  1987
UNITEDS  1988
UNITEDS  1989
UNITEDS  1990
UNITEDS  1991
UNITEDS  1992
UNITEDS  1993
UNITEDS  1994
UNITEDS  1995
UNITEDS  1996
UNITEDS  1997
UNITEDS  1998
UNITEDS  1999
UNITEDS  2000
UNITEDS  2001
UNITEDS  2002
UNITEDS  2003
UNITEDS  2004
UNITEDS  2005
UNITEDS  2006
UNITEDS  2007
UNITEDS  2008
UNITEDS  2009
;
RUN;

The above yields:

jimbarbour_0-1623595459473.png

Note that 1989 is in the data in this scenario.  Previously you had mentioned that 1989 was not to be included.

 

Jim

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 6 replies
  • 948 views
  • 5 likes
  • 4 in conversation