**SAS Language for Dummies – Four Basic Programming Methods**
The SAS language for dummies should start with various transformations. Among these, the following play a significant role, writes xrust:
– **Converting character values to numeric.** This is the most common task for new users. Data may contain character values that look like numbers but are not stored as actual numeric values, so you can’t use them in calculations. To convert them, simply use the `input` function to read and interpret the value using a SAS informat, which specifies how SAS should read it:
“`
data conversion;
length numvar 8;
numvar = input(“$100,99”, dollar6.2);
run;
“`
In this example, the `dollar6.2` informat tells SAS how to interpret the value (a total of six characters, with two decimal places, and space for a currency symbol).
– **Converting character values to date values.** This is just a more complex case of the first tip since a date value in SAS is a number. To convert to a date, you apply the `input` function with a date informat:
“`
data conversion;
length dateval 8;
dateval = input(“10JAN2025”, date9.);
run;
“`
– **Calculating a new date value relative to a given date.** When a date value is a number, you can use the `intnx` and `intck` functions to perform “date math.” For example, this program calculates a new date that is 60 days from the starting date and then counts how many Mondays fall within that interval:
“`
data conversion;
length datevar 8 nextdate 8;
format datevar date9. nextdate date9.;
datevar = ’01JAN2025’d;
nextdate = intnx(‘day’, datevar, 60);
mondays = intck(‘week1.1’, datevar, nextdate);
run;
“`
– **Generating a random number.** The `rand` function is a universal tool for generating random numbers with any distribution or range. For example, to generate a random number between 1 and 100, use:
“`
x = rand(‘integer’, 1, 100);
“`
– **Executing a SAS function outside a DATA step.** SAS functions are typically used to calculate and transform values in a DATA step. You can use the `%sysfunc` macro function to call a SAS function anywhere in open code. For example, to display the current time in a `title` statement, use something like this:
“`
title “Result obtained at %sysfunc(time(), timeampm.)”;
“`
The `%sysfunc` function also allows you to apply a SAS format to the result (`timeampm` in this example).
https://xrust.ru/
https://xrust.ru/news/311286-jazyk-sas-dlja-chajnikov-chetyre-osnovnyh-metodov-programmirovanija.html
https://xrust.ru/news/311286-jazyk-sas-dlja-chajnikov-chetyre-osnovnyh-metodov-programmirovanija.html