Hi Experts,
I am using below code but its not giving both by group value.
data list;
input Name$1-10 Class$ List_price ;
datalines;
Black Bora Track 796
Black Bora Road 399
Black Bora Track 1130
Black Bora Road 1995
Scirocco Mountain 899
Scirocco Mountain 459
Scirocco Road 2256
Scirocco Road 559
;
run;
proc sort data = list out=list1;
by Name Class;
run;
proc print data = list1;
run;
data final;
set list1;
by name class;
if first.name and first.class then List_price1=List_price;
else List_price1=0;
run;
Getting attached result. I simply need the List_price for both by group (name and class).
HAVE | ||
Name | Class | List_Price |
Black Bora | Track | 796 |
Black Bora | Track | 399 |
Black Bora | road | 1130 |
Black Bora | Road | 1995 |
Scirocco | Mountain | 899 |
Scirocco | Mountain | 459 |
Scirocco | Road | 2256 |
Scirocco | Road | 559 |
WANT | ||
Name | Class | List_Price |
Black Bora | Track | 796 |
Black Bora | Track | 0 |
Black Bora | road | 1130 |
Black Bora | Road | 0 |
Scirocco | Mountain | 899 |
Scirocco | Mountain | 0 |
Scirocco | Road | 2256 |
Scirocco | Road | 0 |
thanks
Try first.class only:
data final;
set list1;
by name class;
if first.class then List_price1=List_price;
else List_price1=0;
run;
Hi bobpep212,
this doesn't solve the problem. Need all list prices for all Class within a Name like mentioned the WANT result.
I'm confused then. My output looks like your want below. It shows the list price for the first instance of that class for each Name group. All others list prices would be 0 as List_price1. (With the exception of the point Reeza makes about the road=1130, since 1130 is with the class 'Track' in your have dataset). It doesn't show all the list prices for all the class within a name because your program says not to do that with the else statement.
Actually this code is not working for my original dev code.
Please define what not working means. Is it giving you an error?
@Rahul_SAS wrote:
Actually this code is not working for my original dev code.
assuming sets of 2
data HAVE;
input Name & $10. Class $ List_Price;
cards;
Black Bora Track 796
Black Bora Track 399
Black Bora Road 1130
Black Bora Road 1995
Scirocco Mountain 899
Scirocco Mountain 459
Scirocco Road 2256
Scirocco Road 559
;
data want;
set have;
by name class notsorted;
if last.class then list_price=0;
run;
First, clean your data since road does not equal Road.
Then sort
Then use BY grroup to set to missing/0.
How is road 1130? See red section below. And with two prices which do you want to keep?
proc sort data=list;
by name class descending list_price;
run;
data want;
set list;
if not first.class then list_price=0;
run;
@Rahul_SAS wrote:
Hi Experts,
I am using below code but its not giving both by group value.
data list;
input Name$1-10 Class$ List_price ;
datalines;
Black Bora Track 796
Black Bora Road 399
Black Bora Track 1130
Black Bora Road 1995
Scirocco Mountain 899
Scirocco Mountain 459
Scirocco Road 2256
Scirocco Road 559
;
run;proc sort data = list out=list1;
by Name Class;
run;proc print data = list1;
run;data final;
set list1;
by name class;
if first.name and first.class then List_price1=List_price;
else List_price1=0;
run;
Getting attached result. I simply need the List_price for both by group (name and class).
HAVE Name Class List_Price Black Bora Track 796 Black Bora Track 399 Black Bora road 1130 Black Bora Road 1995 Scirocco Mountain 899 Scirocco Mountain 459 Scirocco Road 2256 Scirocco Road 559
WANT Name Class List_Price Black Bora Track 796 Black Bora Track 0 Black Bora road 1130 Black Bora Road 0 Scirocco Mountain 899 Scirocco Mountain 0 Scirocco Road 2256 Scirocco Road 0 thanks
lets keep it simple.
HAVE
Name | Class | List_Price |
Black Bora | Track | 796 |
Black Bora | Track | 796 |
Black Bora | road | 796 |
Black Bora | Road | 796 |
Scirocco | Mountain | 899 |
Scirocco | Mountain | 899 |
Scirocco | Road | 899 |
Scirocco | Road | 899 |
WANT
Name | Class | List_Price |
Black Bora | Track | 796 |
Black Bora | Track | 0 |
Black Bora | road | 796 |
Black Bora | Road | 0 |
Scirocco | Mountain | 899 |
Scirocco | Mountain | 0 |
Scirocco | Road | 899 |
Scirocco | Road | 0 |
Need value for first row of all class subgroup within name.
HI @Rahul_SAS Reeza was referring to perhaps the typo
in
Black Bora | road | 796 |
Black Bora | Road | 0 |
Also, it seems you apparently didn't test my code. May i request you to test plz
data have;
input Name$1-10 Class$ List_price ;
datalines;
Black Bora Track 796
Black Bora Track 796
Black Bora Road 796
Black Bora Road 796
Scirocco Mountain 899
Scirocco Mountain 899
Scirocco Road 899
Scirocco Road 899
;
run;
proc sort data = have;
by Name Class;
run;
data want;
set have;
by name class;
if first.class then List_price1=List_price;
else List_price1=0;
run;
This isn't exactly what you want?
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.