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;
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.
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;
@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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.