DATA Step, Macro, Functions and more

way to simplify the code

Reply
Contributor
Posts: 39

way to simplify the code

[ Edited ]

Hi ,

 

I have a dataset A and need to create multiple datasets based on the compno ,lobcod and dirsection values.

 

I want to simplify the below steps instead of creating multiple lines.

 

 

if compno in ('02','34') and lobcod in ('FDA','SHP') and dirsection='NP' then do; ptype='NP'; output NP; end;

if compno in ('02','34') and lobcod in ('FDA','SHP') and dirsection='ADHS' then do; ptype='ADHS'; output ADHS; end;

if compno in ('02','34') and lobcod in ('FDA','SHP') and dirsection='aadh' then do; ptype='AADH'; output AADH; end;

 

Thanks in Advance.

if compno in ('02','34') and lobcod in ('FDA','SHP') and dirsection='CDPAS' then do; ptype='CDPAS'; output CDPAS; end;

Super User
Posts: 23,963

Re: way to simplyfy the code

You're creating your groups based on the dirsection variable, except you're making it capital?

 

 

 

if compno in ('02','34') and lobcod in ('FDA','SHP') and dirsection='NP' then do; ptype='NP'; output NP; end;
if compno in ('02','34') and lobcod in ('FDA','SHP') and dirsection='ADHS' then do; ptype='ADHS'; output ADHS; end;
if compno in ('02','34') and lobcod in ('FDA','SHP') and dirsection='aadh' then do; ptype='AADH'; output AADH; end;

is the same as:

 

if compno in ('02','34') and lobcod in ('FDA','SHP') then ptype=upcase(dirsection);

Once that's sorted out, you can use any of the methods illustrated here:

 

https://blogs.sas.com/content/sasdummy/2015/01/26/how-to-split-one-data-set-into-many/

 

 

But, one big caveat, in general this is not a good way to store data in SAS. If you're trying to separate data for analysis or for exporting to separate files there are other, better, ways.

PROC Star
Posts: 622

Re: way to simplyfy the code

If your trying to output all distinct dirsection into separate individual datasets then something like this works.

 

proc sql;
select distinct dirsection into:dirsection separated by " "
	from have;
quit;

data &dirsection ;
set have;
do i=1 to countw(&dirsection);
%let Val=scan(&dirsection,i);
if compno in ('02','34') and lobcod in ('FDA','SHP') and upcase(dirsection)=upcase("&val") 
		then do;
			ptype=dirsection;
			output &Val;
			end;
end;
run;
Thanks,
Suryakiran
PROC Star
Posts: 622

Re: way to simplyfy the code

Posted in reply to SuryaKiran

@MarkWik

 

@SuryaKiran  Your proposed solution on the thread https://communities.sas.com/t5/Base-SAS-Programming/way-to-simplyfy-the-code/m-p/447544#M112453  doesn't work either as you have gotten the macro execution and sas execution comepletely wrong. I don't want mean to sound strong/harsh but I have to concur with @tomrvincent  in the objective that it may mislead for the OP and other readers 


@SuryaKiran wrote:

If your trying to output all distinct dirsection into separate individual datasets then something like this works.

 

proc sql;
select distinct dirsection into:dirsection separated by " "
	from have;
quit;

data "&dirsection" ;
set have;
do i=1 to countw("&dirsection");
%let Val=scan("&dirsection",i);
if compno in ('02','34') and lobcod in ('FDA','SHP') and upcase(dirsection)=upcase("&val") 
		then do;
			ptype=dirsection;
			output &Val;
			end;
end;
run;


I agree with you, I just gave an approach to handle and didn't test because I don't have sample data or time. Thanks for pointing out. 

Thanks,
Suryakiran
Ask a Question
Discussion stats
  • 3 replies
  • 122 views
  • 2 likes
  • 3 in conversation