Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is there similar SAS code???

Status
Not open for further replies.

mikeymay

Programmer
May 2, 2007
10
GB
Is there similar code that performs the 'Select Case' function as per VB (Visual Basic)?

In VB this is useful to use instead of multiple nested 'If' statements.

Have a need for a similar thing now in SAS.......
 
Yes, Only it is called select and when


Code:
data;
length x $15;
input val @@;
select (val);
   when(1) x = 'One';
   when(2) x = 'Two';
   when(3,4) x = 'Three or Four';
   otherwise x = 'Something else';
end;
cards;
1 2 3 4 5 6 7
;run;
proc print;run;
 
Thanks for the code kdt82.

I am a little confused by the following bits of code though

length x $15;
input val @@;
cards;
1 2 3 4 5 6 7;

Any chance you could give me a brief prompt for each one?


Thanks
 
Yes sure.

The first line is the length statement telling SAS to create a space in the PDV that is 17 bytes long (SAS stores each letter in 1 byte of memory) and for character data. If you don't specify the length then the length is defined by the first string read in, which in this case is 'One'. This would cause a length of 3 bytes to be assigned to x and all subsequent values of x, be truncated to 3 bytes.

If you don't know what the PDV is or if you just want a better understanding of the SAS datastep here is a walkthrough:
Code:
length x $15;

The following part tells SAS that a variable 'val' is going to be read in as a standard numeric - which will be assigned the default of 8 bytes for numeric data. The double trailing @'s are used to hold the input record for the execution of the next input statement across iterations of the DATA step. I find it useful for saving space when writing code that contains few variables with many observations.

Code:
input val @@;

Finally, the cards statement is same as the datalines statement; it specifies that data is to be read in. The only difference between datalines and cards is the name. Cards has been used since the days of yore when computers where programmed using punch cards. I think SAS just decided it didn't make sense to newer programmers so gave it a more intuitive name. I personally prefer cards because 4 letters shorter - yes, I really am that lazy ;)

Code:
cards;
1 2 3 4 5 6 7;
 
Fantastic!!

Makes much more sense now. Must remeber to keep taking off my VB head when looking at SAS in future.

Again, many thanks :eek:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top