BookmarkSubscribeRSS Feed
emaguin
Quartz | Level 8

In spss:

if (any(variablename,value1,value2, ..., valuen)) newvar=17.

 

Is there a sas equivalent? What is it?

 

secondary question

how would i even go about finding the answer to this question in the sas help system?

Gene Maguin

7 REPLIES 7
Reeza
Super User

What does ANY do?

 

EDIT: According to Google,

 

WHICHC, WHICHN or IN operator (varname in (var1, var2, var3...)

 

 

PaigeMiller
Diamond | Level 26

Please explain what this SPSS function does.

--
Paige Miller
Reeza
Super User

Answer to #2, look up the functionality, find the key words, ie Search variables.
Go to the SAS Functions by Category Page, search by key word and see which functions are similar and may work for what you need.

ballardw
Super User

@emaguin wrote:

In spss:

if (any(variablename,value1,value2, ..., valuen)) newvar=17.

 

Is there a sas equivalent? What is it?

 

secondary question

how would i even go about finding the answer to this question in the sas help system?

Gene Maguin


IIRC my SPSS syntax correctly then your example of searching if a specific variable has any of a list of values you want the IN operator.

/* for character values*/

If charactervariablename in ('Value1' 'Value2' 'Value3' ... 'ValueN') then NewVar=17;

/* for numeric values */

if numvarname in ( 123   4.5  0.0003) then NewVar=17;

The lists of values may be comma or space delimited.

There is a special form for integer values only

if numvar in (2:15) then <do whatever>;

where the : creates a list of sequential integers, in this case from 2 to 15. The sequential list may be combined with other integer sequences or lists of specific non-integer values.

 

Caution with this: Remember the value you display will have a format that may appear to match a value in your list but the value not the formatted value is used by the IN operator. Example:  the actual value 3.0000000001, displayed with a BEST5. format, will not match 3 as the value of an IN operation. So check assigned formats if you think a value should match but isn't. Character values may be sneakier if you have formats that display values shorter than the full length.

emaguin
Quartz | Level 8
You remember correctly! I have Cody's book and the IN function is described there. I wasn't thinking about the general problem of which this is an instance.
Thank you for the additional comment and I have a two part follow-up question. Part 1. Since you've used spss, you know that Spss has fortran-like numeric formats, e.g., F10.4, that control the display of numeric data but not its stored value. In your comment you refer to BEST5. When I convert a string number, e.g., "23", to numeric, I write, input(D1LIVING,BEST32.). What BEST32 or BEST5 means? No idea. No idea what sas calls this topic. No idea where it would be discussed. Would you point me to the topic/location please.
Part 2. You point out that character values may be sneakier. In spss the statement
if (x3 eq 'qq123') y=1. Where x3 is an A7 (Fortran meaning of A) variable.
Spss right pads the shorter with spaces, 'qq123' to 'qq123 ', the value of shorter string. So: what does sas do in the situation?
Thank you.


Tom
Super User Tom
Super User

SAS has two data types. Floating point numbers and fixed length character strings.  The best way to define whether your variable is numeric or character (and if character how many bytes it can hold) is with the LENGTH statement.  Floating point numbers are 8 bytes long so in general it is best to use a length of 8 for them (shorter length means less precision).  Use of a $ in front of the length will indicate a character variable.  Maximum length of a character variable is 32,767 bytes.  If you don't define your variables (or read them from an input dataset) before using them in other statements, such as assignment statements or INPUT or FORMAT or INFORMAT statements, then SAS will make its best guess how to define the variable.

 

SAS uses FORMATS for displaying values as text and INFORMATS for converting text into values.   Numeric formats display numeric values as text.  Character formats display character values as text.  Numeric informats convert text into numeric values.  Character informats convert text into character values.

 

The normal numeric format, usually shown in the documentation as w.d, will display numbers with a fixed total number of characters (the w or width) and a fixed number of character after the decimal place (the d). If the decimal positions are zero or not specified then the decimal point is not added.  The decimal point counts in the total width.  You can also call this format by its alias F.  So F10.4 would mean 5 characters for digits (or negative sign), one character for the decimal point and four characters for digits after the decimal point.

 

BEST is the name of a FORMAT that will attempt to find the "best" way to display a number within the width limit specified.  So it will adjust the number of decimal places included based on the value.  It will even switch to scientific notation if the the value cannot be fit otherwise.

 

For Character variables the names of the formats and informats start with a $.

 

Users can create their own custom formats (and informats) using PROC FORMAT.  The formats are independent from the variables you might want to use them with.  So unlike the SPSS "value label" feature if you have multiple variables that are using the same coded values you only need to define one format and then use it with as many variables are you want.  So if you have a lot of variables coded as 1=Yes and 2=No then define a YESNO format and attach to all of them.

proc format;
values yesno 1='Yes' 2='No';
run;
data want;
  set have;
  format q1-q20 yesno. ;
run;

You can also decide to use a different format for the same variable at different times.  For example the YYMM format will display date values using only the year and month numbers.  So you could generated a summary of data by MONTH by just changing the way the date is displayed (as long as the procedure you are using actually groups by the display values instead of the actual values).

title1 'Summary by YEAR';
proc means data=have;
  class date;
  format date year4. ;
  var income expense ;
run; 

title1 'Summary by YEAR and MONTH';
proc means data=have;
  class date;
  format date yymm7. ;
  var income expense ;
run; 

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

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
  • 7 replies
  • 666 views
  • 3 likes
  • 6 in conversation