Hello everybody,
I have a simple code that is not doing what is supposed to and I am baffled...! Here is the simplified version of the code - I am explaining immediately after
data coun1; set temp05;
if exporter="SAU" or exporter="BHR" or exporter="QAT" or exporter="KWT" or exporter="OMN" or exporter="ARE" then EGCC=1; else EGCC=0;
if importer="SAU" or importer="BHR" or importer="QAT" or importer="KWT" or importer="OMN" or exporter="ARE" then IGCC=1; else IGCC=0;
run;
data coun2; set coun1;
if year=2015 or year =2005; proc sort ;by year;
run;
data igcc2; set coun2;
if egcc=1 and igcc=1;
run;
_____________________
The problem is that the final file (igcc2) looks as follows
which means that it is coding importers such as "ABW" and "AFG" with an IGCC=1.
Could someone explain what is going on here?
Thank you.
Hello,
On the second if condition, you have a copying mistake. you have 'exporter="ARE"' that's the issue
Hello,
On the second if condition, you have a copying mistake. you have 'exporter="ARE"' that's the issue
Please supply your example data in usable form, like this:
data temp05;
input exporter $ importer $;
datalines;
ABW ABW
AFG AFG
SAU SAU
;
Simplify your conditions, by using the IN operator and by using the fact that a boolean true is 1 and false is 0:
data coun1;
set temp05;
egcc = (exporter in ("SAU","BHR","QAT","KWT","OMN","ARE"));
igcc = (importer in ("SAU","BHR","QAT","KWT","OMN","ARE"));
run;
This flags the first two obs of my example dataset with 0's.
Just a small tip
Instead of using multiple or's you can use the 'IN' operator that will make your code more compact.
Example:
data coun1;
set temp05;
if exporter in ("SAU" "BHR" "QAT" "KWT" "OMN" "ARE") then EGCC=1;
else EGCC=0;
if importer in ("SAU" "BHR" "QAT" "KWT" "OMN" "ARE") then IGCC=1;
else IGCC=0;
run;
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.