using select when statements,define a new variable providing the country of origin for each model based on the manufacturer
libname l '/folders/myfolders';
data carselect;
set l.permanent;
select(Manufacturer);
when(Manufacturer="Audi") country="USA";
otherwise country="other";
end;
run;
not getting the desired output,for each model the country should come up
1) add LENGTH COUNTRY $n - statement,
otherwise the length will be according to 1st litteral given ("USA" - 3 characters only)
2) change the when statement to:
when ("Audi") country = "USA";
libname l '/folders/myfolders';
data carselect;
set l.permanent;
select(Manufacturer);
LENGTH country $n;
when("Audi","Acura") country="USA";
otherwise country="other";
end;
run;
log
The length statement goes outside the select-when code block and you need to fill in a value for N, it's not n.
data carselect;
set sashelp.cars;
length country $5;
select(Make);
when("Audi","Acura") country="USA";
otherwise country="other";
end;
run;
and if i want ot include different manufacturers and display the country for them,then how do i do it,
for example for bmw country is germany, how do i do it.Can you give an example
Add another WHEN line.
The documentation has various examples.
libname l '/folders/myfolders';
data carselect;
set l.permanent;
select(Manufacturer);
LENGTH country $10;
when("Audi","Acura") country="USA";
when("Bmw") country="germany";
otherwise country="other";
end;
run;
not getting the output for bmw, it is shwoing country as other only.
Comparison is case sensitive.
Bmw is not the same as BMW.
libname l '/folders/myfolders';
data carselect;
set l.permanent;
select(Manufacturer);
LENGTH country $15;
when("Honda","Lexus","Mitsubis","Infiniti") country="Japan";
when("Hyundai") country=""SouthKorea";
when("BMW","Audi"," Mercedes") country="germany";
when("Jaguar") country="India";
when("Acura","Buick","Cadillac","Chevrole","Chrysler","Dodge","Ford","Jeep","Lincoln") country="USA";
otherwise country="other";
end;
run;
Can you check upon this
Log
Length statement goes outside the select block.
The enhanced editor of SAS quickly shows the location of the error.
When program code suddenly appears in the color of string literals, you have an unmatched quote.
libname l '/folders/myfolders';
data carselect;
set l.permanent;
length country $15;
select(Manufacturer);
when("Honda","Lexus","Mitsubis","Infiniti") country="Japan";
when("Hyundai") country=""SouthKorea";
when("BMW","Audi"," Mercedes") country="germany";
when("Jaguar") country="India";
when ("Acura","Buick","Cadillac","Chevrole","Chrysler","Dodge","Ford","Jeep","Lincoln") country="USA";
otherwise country="other";
end;
run;
vs.
data carselect;
set l.permanent;
length country $15;
select(Manufacturer);
when("Honda","Lexus","Mitsubis","Infiniti") country="Japan";
when("Hyundai") country="SouthKorea";
when("BMW","Audi"," Mercedes") country="germany";
when("Jaguar") country="India";
when("Acura","Buick","Cadillac","Chevrole","Chrysler","Dodge","Ford","Jeep","Lincoln") country="USA";
otherwise country="other";
end;
run;
Two consecutive double quotes before SouthKorea.
And for better readability, DON'T WRITE THE LENGTH STATEMENT INTO THE SELECT BLOCK.
There. You were told three times.
@rishabhmehra13 wrote:
and if i want ot include different manufacturers and display the country for them,then how do i do it,
for example for bmw country is germany, how do i do it.Can you give an example
- increase the length of the target variable so it is sufficient for all counrtry names
- add aditional when clauses within the select block
using select when statements,define a new variable providing the country of origin for each model based on the manufacturer
libname l '/folders/myfolders';
data carselect;
set l.permanent;
select(Manufacturer);
when(Manufacturer="Audi") country="USA";
otherwise country="other";
end;
run;
not getting the desire output for each manufacturer.I want to display country for each manufacturer based on the manufacturer.
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.