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
What does ANY do?
EDIT: According to Google,
WHICHC, WHICHN or IN operator (varname in (var1, var2, var3...)
Please explain what this SPSS function does.
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.
Here's the web link to the SAS doc: SAS Help Center: SAS Help Center: Welcome
@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.
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.