Posts

Options VALIDVARNAME=UPCASE

VALIDVARNAME= V7 UPCASE ANY VALIDVARNAME= option is generally used in SAS whenever we want to control the SAS variable names in the dataset. VALIDVARNAME= V7 UPCASE ANY The default option will be VALIDVARNAME=V7 until we specify as UPPERCASE or ANY . When we mention options VALIDVARNAME=V7 , that means we are telling SAS to change the name of the Database column (etc EXCEL sheet column) to valid SAS name with certain rules keeping in mind. Here are those rules that SAS needs to follow, when it changes the DBMS column name to valid SAS name. Only 32 mixed case (lower or uppercase) characters are allowed in each variable. Names should be starting with an underscore or an alphabet (either uppercase or lower case character). Invalid characters in the DBMS column (ex. $) should be changed to underscores. See the SAS Language Reference: Dictionary to get more details about the rules. VALIDVARNAME=UPCASE When we mention options VALIDVARNAME=UPPERCASE we are telling SAS to change the column...

How to merge data sets with a common variable?

Here is the simple way of merging the data sets with a common variable if the datasets has the same prefix name. For example: col1-col10, dsn1-dsn 7 , or data1 to data10 with common variable of ID. Considering we have 10 datsets and all of them having the same prefix data; %macro mymerge (n); data merged; merge %do i = 1 % to &n; data&i %end; ; /* this additional ';' is necessary, the first ';' is for the "%end", while the second ';' is for "Merge"*/; by id; run; %mend; %mymerge(10)

Merging the data sets using macro code

Merging the data sets with a common variable if the datasets has the same prefix name? For example: col1-col10 dsn1-dsn 7 data1 to data6 with common variable of Usubjid. here is the example, I have 7 datasets i need to merge and each of them having the common variable(usubjid) to merge, and all the datasets having the same prefix dsn(dsn1 to dsn7). % macro allmerge (n); data combine ; merge %do i = 1 % to &n; dsn&i %end; ; /* this additional ';' is necessary, the first ';' is for the "%end", while the second ';' is for "Merge" */ by usubjid; run ; %mend; %allmerge (7)

Resolving and Using &&var&i Macro Variables

Here are the 2 important questions always comes up in our minds ,(& vs && vs &&& and single dot and double dots ) when we are dealing with macros for the first time and here are the answers for them. I did find a very good regarding the above topics in the one of the SAS forums and IAN WHITLOCK explained it very clear. direct link : or when to use &,&&,and &&&,how do we distinguish? &MACVAR references a macro variable. The rule is that the scanner reads from left to right. Two ampersands resolve to one and scanner continues. If the resulting expression has ampersands then that expression is scanned again. So &&x resolves to &x resolves to value of X &&&x resolves to & followed by the value of X which then resolves to the value of the variable indicated. If you are familiar with TSO CLISTS (even if you are not), they are very similar to SAS macro. SAS was originally based on PL1, so both SAS syntax an...

How can I count number of observations per subject in a data set?

Image
We always have this question in mind, while we do the SAS programming and here is the simple answer for that, we just need to use SUM statement and the FIRST .variable in the SET statement and then the RETAIN statement to calculate the observations count per subject. By doing some minor modification we can calculate observations count per subject per visit also. (Just include visit variable in the BY variable list in PROC sort and First . variable list in datastep with SET statement). For example: data dsn ; input patid implants ; datalines; 1 3 1 1 1 2 1 1 2 1 2 2 3 1 4 2 3 1 4 5 2 3 1 6 ; run ; proc sort data=dsn ; by patid ; run ; data dsn1 ; set dsn ; by patid ; cnt+1 ; if first.patid then cnt=1 ; run ; proc sort data= dsn1 ; by patid descending cnt ; run ; data dsn2 ; set dsn1 ; by patid ; retain totcnt ; if first.patid then totcnt=cnt ; output; run; proc print data=dsn2 ; run; Output:

Mastering Duplicates Removal in SAS: A Comprehensive Guide to Using PROC SQL, DATA STEP, and PROC SORT

Removing Duplicate Observations in SAS: A Comprehensive Guide Removing Duplicate Observations in SAS: A Comprehensive Guide In data analysis, it's common to encounter datasets with duplicate records that need to be cleaned up. SAS offers several methods to remove these duplicates, each with its strengths and suitable scenarios. This article explores three primary methods for removing duplicate observations: using PROC SQL , the DATA STEP , and PROC SORT . We will provide detailed examples and discuss when to use each method. Understanding Duplicate Observations Before diving into the methods, let's clarify what we mean by duplicate observations. Duplicates can occur in different forms: Exact Duplicates: All variables across two or more observations have identical values. Key-Based Duplicates: Observations are considered duplicates based on the values of specific key variables (e.g., ID, Date). The ...

How to scan more than 20 records to determine variable attributes

Usage Note 1075: How to scan more than 20 records to determine variable attributes in EFI In Versions 7 and 8 of the SAS System, by default the Import Wizard, PROC IMPORT and the External File Interface (EFI) scan 20 records to determine variable attributes when reading delimited text files. Changing the default setting can only be done for EFI in Version 7, Release 8 and Release 8.1. Beginning in Release 8.2 changing the default setting is applicable to the Import Wizard, PROC IMPORT and EFI. Follow the steps below to change the default behavior: 1) Type regedit on the command line (white box with a check mark) 2) When the Registry Editor window opens, double click on the PRODUCTS icon 3) Double click on the BASE icon 4) Double click on the EFI icon 5) In the window on the right the Contents of EFI will be populated with EFI options 6) Double click on GuessingRows 7) When the new window opens with the old value of 20 , delete it, enter the new value, and clic...