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

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 del.jpg

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.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Athenkosi
Obsidian | Level 7

Hello,

 

On the second if condition, you have a copying mistake. you have 'exporter="ARE"' that's the issue

View solution in original post

5 REPLIES 5
Athenkosi
Obsidian | Level 7

Hello,

 

On the second if condition, you have a copying mistake. you have 'exporter="ARE"' that's the issue

Dim13
Obsidian | Level 7
Thank you!!!.... I guess staring at this for the last 3 hours did not help! It is always the simplest problems
thanks again.
Kurt_Bremser
Super User

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.

Dim13
Obsidian | Level 7
Thank you. Both solutions worked fine.
Athenkosi
Obsidian | Level 7

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;

 

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1384 views
  • 3 likes
  • 3 in conversation