BookmarkSubscribeRSS Feed
cho16
Obsidian | Level 7

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;

3 REPLIES 3
Reeza
Super User

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.

SuryaKiran
Meteorite | Level 14

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
SuryaKiran
Meteorite | Level 14

@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

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 3 replies
  • 1327 views
  • 2 likes
  • 3 in conversation