BookmarkSubscribeRSS Feed
nicnad
Fluorite | Level 6

Hi,

I have the following SAS code. As of right now, it displays each variables from mylib.test table + variables count_gafi, count_sanction and count_person. How can I make sure to only display the variables count_gafi, count_sanction and count_person

Here is the code. Thank you for your help and time.  :

data work.ind14_15_16;
set mylib.test end=eof;
by objet;
retain count_gafi count_sanction count_person ;

switch = "n";

if _n_ eq 1 then do;

     count_gafi=0;

     count_sanction=0;

     count_person=0;
end;

if index(lowcase(strip(objet)),'bolivie')>0 or
index(lowcase(strip(objet)),'équateur')>0 or
index(lowcase(strip(objet)),'equateur')>0 or
index(lowcase(strip(objet)),'éthiopie')>0 or
index(lowcase(strip(objet)),'ethiopie')>0 or
index(lowcase(strip(objet)),'indonésie')>0 or
index(lowcase(strip(objet)),'indonesie')>0 or
index(lowcase(strip(objet)),'kenya')>0 or
index(lowcase(strip(objet)),'nigeria')>0 or
index(lowcase(strip(objet)),'nigéria')>0 or
index(lowcase(strip(objet)),'pakistan')>0 or
index(lowcase(strip(objet)),'sao')>0 or
index(lowcase(strip(objet)),'sri lanka')>0 or
index(lowcase(strip(objet)),'thailand')>0 or
index(lowcase(strip(objet)),'tailand')>0 or
index(lowcase(strip(objet)),'turquie')>0 or
index(lowcase(strip(objet)),'tanzanie')>0 or
index(lowcase(strip(objet)),'viet')>0 or
index(lowcase(strip(objet)),'yemen')>0 or
index(lowcase(strip(objet)),'yémen')>0  then do;
count_gafi=count_gafi+1;
switch = "y";
end;

if index(lowcase(strip(objet)),'bélarus')>0 or
index(lowcase(strip(objet)),'belarus' )>0 or
index(lowcase(strip(objet)),'corée du nord' )>0 or
index(lowcase(strip(objet)),'rpdc' )>0 or
index(lowcase(strip(objet)),'coree du nord')>0 or
index(lowcase(strip(objet)),'ivoire'  )>0 or
index(lowcase(strip(objet)),'congo' )>0 or
index(lowcase(strip(objet)),'chine' )>0 or
index(lowcase(strip(objet)), 'cuba'  )>0 or
index(lowcase(strip(objet)),'érythrée' )>0 or
index(lowcase(strip(objet)), 'erythree')>0 or
index(lowcase(strip(objet)), 'iran'  )>0 or
index(lowcase(strip(objet)),'iraq' )>0 or
index(lowcase(strip(objet)), 'liberia'  )>0 or
index(lowcase(strip(objet)),'libéria' )>0 or
index(lowcase(strip(objet)),'libye'  )>0 or
index(lowcase(strip(objet)),'myanmar' )>0 or
index(lowcase(strip(objet)),'birmanie')>0 or
index(lowcase(strip(objet)), 'somalie'  )>0 or
index(lowcase(strip(objet)),'sierra' )>0 or
index(lowcase(strip(objet)), 'soudan')>0 or
index(lowcase(strip(objet)), 'syrie' )>0 or
index(lowcase(strip(objet)),'zimbabwe')>0
then do;
count_sanction=count_sanction+1;
switch = "y";
end;

if switch ="n" then do;
count_person = count_person + 1; /* If the record is not part of count_gafi or count_sanction, I want it to be part of count_person */
end;
if eof;
run;

5 REPLIES 5
Linlin
Lapis Lazuli | Level 10

changing

data work.ind14_15_16;

to

data work.ind14_15_16(keep=count_:);

nicnad
Fluorite | Level 6

Thank you for the quick reply linlin.

Exactly what I needed.

Also, would you know a more efficient method to perform the count above?


Linlin
Lapis Lazuli | Level 10

I don't know. Sorry:smileysilly:!

Astounding
PROC Star

You could improve the speed by reducing the number of function calls.  Try adding this statement:

newvar = lowcase(strip(objet));

Then refer to NEWVAR inside all the INDEX functions.

You could restrict the variables you read in on the SET statement:

set mylib.test (keep=objet) end=eof;

More complex, and possibly beyond your skill level, it appears your data are sorted by objet.  In that case, you could compute flags for the first observation for each value of objet.  The flags would indicate whether count_gafi or count_sanction should be incremented for that value of objet (and whether switch should be y or n).  Then all the remaining observations for the same value of objet could use the flags, instead of recomputing using a ton of functions for each observation that has the same value of objet.

Good luck.

nicnad
Fluorite | Level 6

Thank you very much for the recommandation.

Best regards.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 770 views
  • 0 likes
  • 3 in conversation