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.
@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;
@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
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.
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.
@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;
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:
Note that 1989 is in the data in this scenario. Previously you had mentioned that 1989 was not to be included.
Jim
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: