DATA Step, Macro, Functions and more

Creating Output dataset using Macro

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

Creating Output dataset using Macro

Hi,

 

I am a beginner in SAS.

I have a dataset carList that contains a list of cars and their details. I want to create output datasets based on their color, USING MACRO.

I cannot make my code work. I was able to create the data sets but the segregation is not working. 

I would really appreciate if someone could help me. 

 

Here's my code, I believe the error is with the 'output' part. I wanted to create an array for the colors using %let but couldn't figure out. (yes, I googled). Thanks in advance!!

 

%let color1 = BLACK;
%let color2 = BLUE;
%let color3 = BROWN;
%let color4 = RED;
%let color5 = GRAY;
%let color6 = SILVER;
%let color7 = WHITE;
%let color8 = YELLOW;
%let colorData1 = BLACK_Vehicles_try;
%let colorData2 = BLUE_Vehicles_try;
%let colorData3 = BROWN_Vehicles_try;
%let colorData4 = RED_Vehicles_try;
%let colorData5 = GRAY_Vehicles_try;
%let colorData6 = SILVER_Vehicles_try;
%let colorData7 = WHITE_Vehicles_try;
%let colorData8 = YELLOW_Vehicles_try;


data xxxxxx.carList;
length make $ 10;
infile '/home/xxxxxx/carList.txt' dlm=',';
input make $
name $
type $
color $
year
price;

run;


%macro createm;
%do i=1 %to 3;
data xxxxxx.&&colorData&i;
set xxxxxx.carlist;
%do i=1 %to 3;
%if color = &&ccolor&i %then %output xxxxxx.&&colorData&i;
%end;
run;
%end;
%mend;

%createm;


Accepted Solutions
Solution
a week ago
Frequent Contributor
Posts: 77

Re: Creating Output dataset using Macro


data CarList;
infile "Path\CarList.txt" dlm = ',';
input Brand $ : 20. Name $ : 20. Type $ : 15. Color $ Year Price;
run;

proc sql;
select count(distinct color) into :ClrCnt from CarList;
%let ClrCnt = %cmpres(&ClrCnt);
select distinct Color into :Clr1 - :Clr&ClrCnt from CarList;
quit;

%Macro CarList;
%do i = 1 %to &ClrCnt;
data &&Clr&i.._Vehicles_Try;
set CarList;
if color = "&&Clr&i";
run;
%end;
%Mend;
%CarList;

View solution in original post


All Replies
Regular Contributor
Posts: 167

Re: Creating Output dataset using Macro

[ Edited ]

not sure why you have two do loops and they use the same index "I". Why not just:

 

%macro test(col);

data ...;

set .... (where=(colour=&col))

 

etc

 

run;

%mend;

 

%test(WHITE);

%test(BLACK);

etc

--------------
blog: papersandprograms.com
Regular Contributor
Posts: 213

Re: Creating Output dataset using Macro

Same procedure as every week ...  Data should not be stored in macro variables, data is best held in datasets. So please explain what you have and what you need as result.

Solution
a week ago
Frequent Contributor
Posts: 77

Re: Creating Output dataset using Macro


data CarList;
infile "Path\CarList.txt" dlm = ',';
input Brand $ : 20. Name $ : 20. Type $ : 15. Color $ Year Price;
run;

proc sql;
select count(distinct color) into :ClrCnt from CarList;
%let ClrCnt = %cmpres(&ClrCnt);
select distinct Color into :Clr1 - :Clr&ClrCnt from CarList;
quit;

%Macro CarList;
%do i = 1 %to &ClrCnt;
data &&Clr&i.._Vehicles_Try;
set CarList;
if color = "&&Clr&i";
run;
%end;
%Mend;
%CarList;

Respected Advisor
Posts: 3,067

Re: Creating Output dataset using Macro

There is rarely a good reason to split up a dataset like this. Usually, it is a bad idea that causes more programming to be needed. In this case, it causes the macro to be needed, when most likely it is not needed. Just about any analysis can be performed on the entire data set, and you can us a BY statement in any SAS PROC to work on each color (again, macro not needed for this).

--
Paige Miller
Super User
Posts: 6,785

Re: Creating Output dataset using Macro

[ Edited ]

I promise, there will be one form of an answer at the end of all this.

 

While there are several mistakes to consider (for example, there is no such thing as %output), the fundamental mistake you are making is attempting to use macro language before you are ready.  Macro language doesn't process data.  It constructs a program.  As a relative beginner,  you need more experience with SAS language to be able to picture the program that needs to be constructed. 

 

The warnings that others have posted about this particular application possibly being a bad idea ... well they might be right.  But I'm going to assume you are doing this for purposes of getting some experience with macro language rather than constructing a useful application.

 

To see the program  you are actually generating, add this to  your code:

 

options mprint;

 

Here's an untested version of the macro that should come pretty close to the mark:

 

%macro createm;

%local k;
data %do k=1 to 3;

               xxxxxx.&&colorData&k

        %end;

;
set xxxxxx.carlist;
%do k=1 %to 3;
   if color = "&&ccolor&k" then output xxxxxx.&&colorData&k;

   %if &k < 3 %then else;
%end;
run;
%mend createm;

%createm

New Contributor
Posts: 2

Re: Creating Output dataset using Macro

Hello, 

 

Thanks for all the help.

 

We were required to use macros as part of the lesson.

 

I was able to achieve the idea i had in my noob mind using the following code:

 
%macro createm(ind);
data xxxxx.&&ccolor&ind.._Vehicle;
set xxxxx.carlist(where=(color="&ccolor&ind."));
run;
%mend;

%macro categ;
%do i=1 %to 8;
%createm(&i);

proc print data = xxxxx.&&ccolor&i.._Vehicle;
run;

%end;
%mend;

%categ;

 

Again, thank you for all the help! Smiley Very Happy

☑ This topic is solved.

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

Discussion stats
  • 6 replies
  • 178 views
  • 0 likes
  • 6 in conversation