Help using Base SAS procedures

Splitting file based on a variable

Accepted Solution Solved
Reply
Contributor
Posts: 33
Accepted Solution

Splitting file based on a variable

Hi,

I need help with making multiple files from my dataset. This is how the dataset looks like:

Community Name               Time                  Values

CommunityA                       June                    56

CommunityB                       May                     94

CommunityC                       April                     54

CommunityD                      December             10

and so on....

I want to make separate files for each community with thier name identified in the file and rest of the communitites will be blinded.

For eg: Using the dataset above, I want to make 4 different files for each community like this:

For communityA:

Community Name               Time                  Values

CommunityA                      June                    56

2                                       May                     94

3                                        April                     54

4                                        December             10

The file for communityA will have data for the rest of the communities except the community name is blinded and replaced by 2,3,4.

Similarly the file for CommunityB will look like,

Community Name               Time                  Values

1                                         June                    56

CommunityB                        May                     94

3                                        April                     54

4                                        December             10

and so on...

Is this possible in SAS? I would greatly appreciate your time and help regarding the same.


Accepted Solutions
Solution
‎01-29-2013 05:43 AM
Regular Contributor
Posts: 195

Re: Splitting file based on a variable

Hi,

Try the following code in addition of Proc Format.....

data commA commB commC commD;

      set test;

      if community_name EQ "communityA" then output commA;

      if community_name NE "communityA" then do;

         community_name = _N_;

         output commA;

      end;

      if community_name EQ "communityB" then output commB;

      if community_name NE "communityB" then do;

         community_name = _N_;

         if _N_ = '2' then community_name = "communityB";

         output commB;

     end;

      if community_name EQ "communityC" then output commC;

      if community_name NE "communityC" then do;

         community_name = _N_;

         if _N_ = '3' then community_name = "communityC";

         output commC;

     end;

      if community_name EQ "communityD" then output commD;

      if community_name NE "communityD" then do;

         community_name = _N_;

         if _N_ = '4' then community_name = "communityD";

         output commD;

     end;

run;

If there is many community (say more than 50) then process the same logic with iterative %DO loop within Macro Definition...

Cheers

View solution in original post


All Replies
Super User
Posts: 10,538

Re: Splitting file based on a variable

How about a solution that doesn't involve any new data? You could use custome formats to accomplish the same thing for most analysis.

Proc format library=work;

value $CommunityA

'Community A' = 'Community A'

'Community B' = '1'

'Community C' = '2'

'Community D' = '3'

'Community E' = '4

;

value $CommunityB

'Community A' = '1'

Community B' = 'Community B'

Community C' = '2'

'Community D' = '3'

'Community E' = '4

;

run;

Then for any report procs use the appropriate format. If there are a large number of communities involved it is likely worth making a control data set for proc format.

Valued Guide
Posts: 2,175

Re: Splitting file based on a variable

If you are creating different physical files or datasets, then it is straightforward.

However, if you want the appearance of a common/shared dataset to depend on the reader of the dataset, then just ensure the formatsearch path for the user has the appropriate "blinding" format for that user/usergroup in the first format catalog named in the format search path option (FMTSEARCH)

NuPur20 seems to be requesting files.

If these are SAS datasets, my approach would be different from the "external files" solution.

Solution
‎01-29-2013 05:43 AM
Regular Contributor
Posts: 195

Re: Splitting file based on a variable

Hi,

Try the following code in addition of Proc Format.....

data commA commB commC commD;

      set test;

      if community_name EQ "communityA" then output commA;

      if community_name NE "communityA" then do;

         community_name = _N_;

         output commA;

      end;

      if community_name EQ "communityB" then output commB;

      if community_name NE "communityB" then do;

         community_name = _N_;

         if _N_ = '2' then community_name = "communityB";

         output commB;

     end;

      if community_name EQ "communityC" then output commC;

      if community_name NE "communityC" then do;

         community_name = _N_;

         if _N_ = '3' then community_name = "communityC";

         output commC;

     end;

      if community_name EQ "communityD" then output commD;

      if community_name NE "communityD" then do;

         community_name = _N_;

         if _N_ = '4' then community_name = "communityD";

         output commD;

     end;

run;

If there is many community (say more than 50) then process the same logic with iterative %DO loop within Macro Definition...

Cheers

Regular Contributor
Posts: 200

Re: Splitting file based on a variable

Here is a General Solution, section 3.3:

http://www.sascommunity.org/wiki/Macro_CallText

Ron Fehd  macro maven

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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