Discover More Tips and Techniques on This Blog

How to determine whether a numeric or character value exists within a group of variables

Using the IN operator to determine whether a numeric or character value exists within a group of variables

When trying to determine whether a specific value exists within a group of variables, a common approach is to associate the variables with an ARRAY and then use a DO loop to loop through every element or variable in the ARRAY. As an example,
here is a segment of code:


array my_array[*] var1 - var10;
do i = 1 to dim(my_array);
if some_value = my_array[i] then found = 'Yes';
end;


A more efficient alternative is to use the IN operator with the name of the ARRAY and avoid using the DO loop. This can be done with both numeric ARRAYS as well as character ARRAYS. Here is a code segment:

array my_array[*] var1 - var10;
if some_value IN my_array then found = 'Yes';


source: http://support.sas.com/kb/33/227.html

How to convert a SAS date to a character variable

/***************************************************************************//*
Title: Convert a SAS date to a character variable *// *
*//* Goal: Use the PUT function to create a character variable from *//*
a SAS date. *//* *//***************************************************************************/

data one;
input sasdate :mmddyy6.;
datalines;
010199;
run;

data two;
set one;
chardate=put(sasdate,mmddyy6.);
run;

/* RESULTS */

Obs sasdate chardate
1 14245 010199


Source: ftp://ftp.sas.com/techsup/download/sample/datastep/convertchar.html

How to convert a character variable that represents a date into a SAS date

Convert a character variable that represents a date into a SAS date

Use the INPUT function to convert a character value that represents a date into a SAS date value.

Data one;
input chardate1 :$6. chardate2 :$9. chardate3 $10. chardate4 :$9.;
datalines;
010199
31dec1999
21/09/2005
5/9/2005; Run;

/* Use the INPUT function to convert a character value that represents a date
*//* into a SAS date value. Choose the second parameter to the INPUT function
*//* based upon what the current character value looks like. Use a FORMAT
*//* statement to apply the date format you want when you are done. *//*


*//* Note: If you are in SAS 9.0 or above, you may prefer using the ANYDTDTEw.
*//* Informat as the second argument to the INPUT function. ANYDTDTEw.
*//* can read multiple date layouts. Refer to the SAS Language Reference,
*//* Dictionary under INFORMATS for more information. */



data two;
set one;
sasdate1=input(chardate1,mmddyy6.);
sasdate2=input(chardate2,date9.);
sasdate3=input(chardate3,ddmmyy10.);
sasdate4=input(chardate4,ddmmyy10.);
format sasdate1 mmddyy10. sasdate2 yymmdd10. sasdate3 date9. sasdate4 monyy7. ;
run;

proc print;
run;


RESULTS:
Obs chardate1 chardate2 chardate3 chardate4 sasdate1 sasdate2 sasdate3 sasdate4 1
01 0199 31dec1999 21/09/2005 5/9/2005 01/01/1999 1999-12-31 21SEP2005 SEP2005
source: http://support.sas.com/kb/24/591.html

LAG Function: How to obtain information from previous observation(s)

Often times SAS® programmers need to retain the value of a variable in the current observation to the next observation. The LAG function  can be very helpful here. A LAGn (n=1-100) function returns the value of the nth previous execution of the function. It is easy to assume that the LAGn functions return values of the nth previous observation.


Using the LAG function to obtain information from previous observation(s)

**********************************************************;/* Sample 1: Create a single lag of one variable */


data one;
input x;
lagonce=lag(x);
datalines;
1
2
3
4
5
;
proc print data=one;
title 'Sample1: Single lag of one variable';
run;

***************************************************************;/* Sample 2: Create multiple lags of one variable */


data two;
input x;
lag1=lag(x);
lag2=lag2(x);
datalines;
1
2
3
4
5
;
proc print data=two;
title 'Sample 2: Multiple lags of one variable';
run;
***************************************************************;/* Sample 3: Create a single lag of one variable within a BY-Group */
/* See also: */
/* Sample 140: Obtaining the previous value of a variable within */a BY-Group */
/* Sample 108: Use the LAG function to conditionally carry */
/* information down a data set */



data three;
input group $ x;
datalines;
a 1
a 2
a 3
b 1
b 2
b 3
b 4
;
data final;
set three;
by group;
lagx=lag(x);
/* Note the LAG function is executed outside the IF condition. */
/* On the first member of the BY-Group, the variable created */
/* with the LAG function is reset to missing. */

if first.group then lagx=.;
run;
proc print data=final;
title 'Sample 3: Single lag of one variable within a BY-Group';
run;

RESULTS:
Sample1: Single lag of one variable
Obs x lagonce
1 1 .
2 2 1
3 3 2
4 4 3
5 5 4

Sample 2: Multiple lags of one variable
Obs x lag1 lag2
1 1 . .
2 2 1 .
3 3 2 1
4 4 3 2
5 5 4 3

Sample 3: Single lag of one variable within a BY-Group
Obs group x lagx
1 a 1 .
2 a 2 1
3 a 3 2
4 b 1 .
5 b 2 1
6 b 3 2
7 b 4 3

source: http://support.sas.com/kb/25/938.html


Without Using LAG Function:
*****************************************************************************;


Example2:
data lagcheck;

input a b ;
datalines;
1 1
. 2
. 3
. 4
. 5
2 6
. 7
. 8
3 9
. 10
. 11
. 12
. 13
. 14
;
run;
*Method1;
data lagcheck;
set lagcheck;
n=_n_;
if missing(a) then do;
do until (not missing(a));
n=n-1;
set lagcheck(keep=a) point=n;
end;
end;
run* Note: Remember 2 Set statements;
**********************************************************;
*Method2;
data lagcheck;
set lagcheck;
retain lasta;
if not(missing(a)) then lasta=a;
if missing(a) then a=lasta;
drop lasta;
run;

***************************************************************;
* Here is another example given in SAS-L archives about Re: A Confusion about how to filling out empty cells with duplicates. and interesting solutiion using UPDATE Statement;

data have;
input Subject number1 number2;
infile datalines truncover;
datalines;
10001 212
10001 . 10
10002 555
10002
10002
10002 . 11
10003 11
10003
10003 . 12
10003

;;;;
run;


data need;
do _n_ = 1 by 1 until(last.subject);
update have(obs=0) have;
by subject;
end;
do _n_ = 1 to _n_;
output ;
end ;
run;
**********************************************************************;

Disclosure:

In the spirit of transparency and innovation, I want to share that some of the content on this blog is generated with the assistance of ChatGPT, an AI language model developed by OpenAI. While I use this tool to help brainstorm ideas and draft content, every post is carefully reviewed, edited, and personalized by me to ensure it aligns with my voice, values, and the needs of my readers. My goal is to provide you with accurate, valuable, and engaging content, and I believe that using AI as a creative aid helps achieve that. If you have any questions or feedback about this approach, feel free to reach out. Your trust and satisfaction are my top priorities.