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

I need to split my dataset into several datasets based on a certain variable. Searching the forums I found this old thread which almost does the exact same:

 

https://communities.sas.com/t5/SAS-Procedures/Splitting-a-dataset-into-multiple-dataset/m-p/399904#M...

 

The difference is that in this thread the procedure is done by looking at one variable to split the dataset and another variable for naming the datasets.

I’m still relatively new to SAS so I don’t fully understand what the code does.

 

 

data have;
infile datalines truncover;
input @1 deptid 1. deptdescription $ & 3-20;
datalines;
1 Maths
2 Astronomy
1 Maths
3 Physics
4 Public Health
3 Physics
;
run;
 
/* Sort by Deptid so first./last. logic can be used in the next step */
proc sort data=have;
by deptid;
run;
 
/* CALL SYMPUTX creates macro variables &Subj1, &Subj2, etc from the values of Deptdescription.*/
/* The total unique values of Deptdescription are stored in &Total                             */
data _null_;
set have;
by deptid;
if first.deptid then do;
i+1;
call symputx('subj'|| left(put(i,2.)),deptdescription);
end;
if last.deptid then call symputx('total',i);
run;
 
/* Within a macro %DO loop the data sets are created and those observations with the same */
/* Deptdescription values are read with a WHERE clause                                    */
%macro test;
%do i=1 %to &total;
data &&subj&i;
set have;
where deptdescription="&&subj&i";
run;
%end;
%mend test;
 
/* invoke the macro */
%test

 

 In my case the data would look something like this:

(I changed the Input Statements for this example accordinlgy I hope. The actual data I'm working with is a SAS dataset)

 

 

data have;
infile datalines truncover;
input @1 dept $1-13 Variable1  4.;
datalines;
Maths 213
Astronomy 34
Maths 43
Physics 34
Public Health 54
Physics 324
;
run;

 

 

I think I Need to change These two lines:

call symputx('subj'|| left(put(i,2.)),deptdescription);

 

where deptdescription="&&subj&i";

However, I  tried several changes such as:

 

call symputx('subj'|| compress(dept),Variable1);

 plus 

 

where variable1="&&subj&i";

But they didn’t work. Note that compress is not an elegant solution, but I needed a quick way to get rid of the space in the dataset Name.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

I too hope that the actual data you work with is a SAS data set.   The INPUT statements you are creating are not going to work.

 

There are a handful of changes to make.  One approach would be to use an additional macro variable.  You now have (and should keep):

 

call symputx('subj'|| left(put(i,2.)),deptdescription);

Add another macro variable at that point in the program:

 

call symputx('dsname'|| left(put(i,2.)),compress(deptdescription));

Then change the macro loop so it refers to both variables:

 

%macro test;
%do i=1 %to &total;
data &&dsname&i;
set have;
where deptdescription="&&subj&i";
run;
%end;
%mend test;

Let me know if you run into any difficulties with that.

 

As a general rule, it's a good idea to avoid macro language when you are just starting to learn SAS.

View solution in original post

4 REPLIES 4
Astounding
PROC Star

I too hope that the actual data you work with is a SAS data set.   The INPUT statements you are creating are not going to work.

 

There are a handful of changes to make.  One approach would be to use an additional macro variable.  You now have (and should keep):

 

call symputx('subj'|| left(put(i,2.)),deptdescription);

Add another macro variable at that point in the program:

 

call symputx('dsname'|| left(put(i,2.)),compress(deptdescription));

Then change the macro loop so it refers to both variables:

 

%macro test;
%do i=1 %to &total;
data &&dsname&i;
set have;
where deptdescription="&&subj&i";
run;
%end;
%mend test;

Let me know if you run into any difficulties with that.

 

As a general rule, it's a good idea to avoid macro language when you are just starting to learn SAS.

Sledgehamma
Fluorite | Level 6

Thank you very much!

I changed the code accordingly but I’m getting the following errors:

 

WARNING: Apparent symbolic reference TOTAL not resolved.

ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was:

       &total

ERROR: The %TO value of the %DO I loop is invalid.

ERROR: The macro TEST will stop executing.

Astounding
PROC Star
Apparently you are not creating &TOTAL. Show the final DATA step (before running the %DO loop).
Sledgehamma
Fluorite | Level 6

I was just about to copy the data step when I noticed that I hadn’t changed two variables….they were still called „deptdescription“ from the early example. No wonder why it didn’t work.

The error wasn’t in the program but in front of the computer. Thanks again for your help, its much appreciated!

 

 

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!
What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 812 views
  • 2 likes
  • 2 in conversation