Write and run SAS programs in your web browser

problem in using select-when

Reply
Contributor
Posts: 31

problem in using select-when

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

 

 

 

Super User
Posts: 1,073

Re: problem in using select-when

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";

 

 

Contributor
Posts: 31

Re: problem in using select-when

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

 

1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
55
56 libname l '/folders/myfolders';
NOTE: Libref L was successfully assigned as follows:
Engine: V9
Physical Name: /folders/myfolders
57 data carselect;
58 set l.permanent;
59
60 select(Manufacturer);
61 LENGTH country $n;
_
391
202
ERROR 391-185: Expecting a variable length specification.
 
ERROR 202-322: The option or parameter is not recognized and will be ignored.
 
62 when("Audi","Acura") country="USA";
63
64
65 otherwise country="other";
66 end;
67
68 run;
 
NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.CARSELECT may be incomplete. When this step was stopped there were 0 observations and 16 variables.
WARNING: Data set WORK.CARSELECT was not replaced because this step was stopped.
NOTE: DATA statement used (Total process time):
real time 0.04 seconds
cpu time 0.04 seconds
 
 
69
70 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
82
Grand Advisor
Posts: 16,267

Re: problem in using select-when

The length statement goes outside the select-when code block and you need to fill in a value for N, it's not n.

Esteemed Advisor
Posts: 5,922

Re: problem in using select-when

data carselect;
set sashelp.cars;
length country $5;
select(Make);
  when("Audi","Acura") country="USA";
  otherwise country="other";
end;
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 31

Re: problem in using select-when

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

 

Grand Advisor
Posts: 16,267

Re: problem in using select-when

Add another WHEN line. 

 

The documentation has various examples. 

http://support.sas.com/documentation/cdl/en/lestmtsref/68024/HTML/default/viewer.htm#p09213s9jc2t99n...

Contributor
Posts: 31

Re: problem in using select-when

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.

Grand Advisor
Posts: 16,267

Re: problem in using select-when

Comparison is case sensitive. 

 

Bmw is not the same as BMW. 

Contributor
Posts: 31

Re: problem in using select-when

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

 

57 data carselect;
58 set l.permanent;
59
60 select(Manufacturer);
61 LENGTH country $15;
62 when("Honda","Lexus","Mitsubis","Infiniti") country="Japan";
63 when("Hyundai") country=""SouthKorea";
__
49
__________
388
76
64 when("BMW","Audi","Mercedes") country="germany";
___ ____________ ________
49 49 49
65 when("Jaguar") country="India";
____________ ________
49 49
NOTE 49-169: The meaning of an identifier after a quoted string might change in a future SAS release. Inserting white space
between a quoted string and the succeeding identifier is recommended.
 
ERROR 388-185: Expecting an arithmetic operator.
 
ERROR 76-322: Syntax error, statement will be ignored.
 
66
67 when("Acura","Buick","Cadillac","Chevrole","Chrysler","Dodge","Ford","Jeep","Lincoln") country="USA";
___ ___ ___ ___ ___ ___ ____________ _____________________
49 49 49 49 49 49 49 49
NOTE 49-169: The meaning of an identifier after a quoted string might change in a future SAS release. Inserting white space
between a quoted string and the succeeding identifier is recommended.
 
68
69
70
71 otherwise country="other";
72 end;
73
74 run;
75
76 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
77 ODS HTML CLOSE;
78 &GRAPHTERM; ;*';*";*/;RUN;
 
NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.CARSELECT may be incomplete. When this step was stopped there were 0 observations and 16 variables.
WARNING: Data set WORK.CARSELECT was not replaced because this step was stopped.
NOTE: DATA statement used (Total process time):
real time 0.06 seconds
cpu time 0.06 seconds
 
78 ! QUIT;
 
79 QUIT;RUN;
80 ODS HTML5 (ID=WEB) CLOSE;
81
82 ODS RTF (ID=WEB) CLOSE;
83 ODS PDF (ID=WEB) CLOSE;
NOTE: ODS PDF(WEB) printed no output.
(This sometimes results from failing to place a RUN statement before the ODS PDF(WEB) CLOSE statement.)
84 FILENAME _GSFNAME;
NOTE: Fileref _GSFNAME has been deassigned.
85 DATA _NULL_;
86 RUN;
 
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds
 
 
87 OPTIONS NOTES STIMER SOURCE SYNTAXCHECK;
88
Grand Advisor
Posts: 16,267

Re: problem in using select-when

Length statement goes outside the select block. 

 

Esteemed Advisor
Posts: 5,922

Re: problem in using select-when

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Esteemed Advisor
Posts: 5,922

Re: problem in using select-when

And for better readability, DON'T WRITE THE LENGTH STATEMENT INTO THE SELECT BLOCK.

There. You were told three times.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Esteemed Advisor
Posts: 5,922

Re: problem in using select-when


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

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 31

problem using select when

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.

 

 

 

Post a Question
Discussion Stats
  • 15 replies
  • 361 views
  • 6 likes
  • 5 in conversation