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

Dear Experts,

 

I want to delete the data in red font by matching D2010-D2013 to P2010-P2013. For example, no data is in D2011, so delete P2011.

 

CountryD2010D2011D2012D2013P2010P2011P2012P2013
A5 10 5600566558006000
B51520 4200425543004355
C3  94255423645604590

 

Then finally want the following table:

 

CountryD2010D2011D2012D2013P2010P2011P2012P2013
A5 10 5600 5800 
B51520 420042554300 
C3  94255  4590

 

Could you please help me, what SAS code I can use?

 

Thanks,

 

Epid

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

One way

 

data have;
input Country $ D2010-D2013 P2010-P2013;
infile datalines dlm='|';
datalines;
A|5|  |10| |5600|5665|5800|6000
B|5|15|20| |4200|4255|4300|4355
C|3|  |  |9|4255|4236|4560|4590
;

data want;
    set have;
    array d D2010-D2013;
    array p P2010-P2013;
    do over d;
        if d = . then p = .;
    end;
run;

View solution in original post

5 REPLIES 5
ed_sas_member
Meteorite | Level 14

Hi @Epid 

 

Here is a way to do that, using arrays: 

 

data have;
	infile datalines dlm="09"x dsd missover;
	input Country $ D2010	D2011	D2012	D2013	P2010	P2011	P2012	P2013;
	datalines;
A	5	 	10	 	5600	5665	5800	6000
B	5	15	20	 	4200	4255	4300	4355
C	3	 	 	9	4255	4236	4560	4590
;
run;

data want;
	set have;
	array _D (*) D2010-D2013;
	array _P (*) P2010-P2013;
	do i=1 to dim(_D);
		if _D(i) = . then _P(i)=.;
	end;
	drop i;
run;
PeterClemmensen
Tourmaline | Level 20

One way

 

data have;
input Country $ D2010-D2013 P2010-P2013;
infile datalines dlm='|';
datalines;
A|5|  |10| |5600|5665|5800|6000
B|5|15|20| |4200|4255|4300|4355
C|3|  |  |9|4255|4236|4560|4590
;

data want;
    set have;
    array d D2010-D2013;
    array p P2010-P2013;
    do over d;
        if d = . then p = .;
    end;
run;
Kurt_Bremser
Super User

By using a better data structure, the code becomes dead simple, and you'll save disk space on top:

(using the example data as provide by the data steps others already posted)

proc transpose data=have out=trans;
by country;
var d: p:;
run;

data have_long;
merge
  trans (rename=(col1=d) where=(substr(_name_,1,1) = 'D'))
  trans (rename=(col1=p) where=(substr(_name_,1,1) = 'P'))
;
period = substr(_name_,2);
keep country period d p;
run;

/* this is all you need with an intelligent structure */
data want_long;
set have_long;
if d ne .;
run;
Epid
Fluorite | Level 6
Thank you very much KurtBremser.
Epid
Fluorite | Level 6
Thank you very much all. All your answers were very helpful.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 5 replies
  • 729 views
  • 0 likes
  • 4 in conversation