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;

 

 

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
  • 5 replies
  • 516 views
  • 3 likes
  • 3 in conversation