BookmarkSubscribeRSS Feed
rishabhmehra13
Calcite | Level 5

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

 

 

 

15 REPLIES 15
Shmuel
Garnet | Level 18

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

 

 

rishabhmehra13
Calcite | Level 5

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
Reeza
Super User

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

rishabhmehra13
Calcite | Level 5

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

 

rishabhmehra13
Calcite | Level 5

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.

Reeza
Super User

Comparison is case sensitive. 

 

Bmw is not the same as BMW. 

rishabhmehra13
Calcite | Level 5

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
Reeza
Super User

Length statement goes outside the select block. 

 

Kurt_Bremser
Super User

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.

Kurt_Bremser
Super User

@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

rishabhmehra13
Calcite | Level 5

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.

 

 

 

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!

SAS Enterprise Guide vs. SAS Studio

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.

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
  • 15 replies
  • 2435 views
  • 6 likes
  • 5 in conversation