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

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;

1 ACCEPTED SOLUTION

Accepted Solutions
MadhuKorni
Quartz | Level 8

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

6 REPLIES 6
pau13rown
Lapis Lazuli | Level 10

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

error_prone
Barite | Level 11

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.

MadhuKorni
Quartz | Level 8

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;

PaigeMiller
Diamond | Level 26

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
Astounding
PROC Star

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

kiukitsu
Calcite | Level 5

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! 😄

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

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
  • 6 replies
  • 7933 views
  • 0 likes
  • 6 in conversation