Sunday, 3 April 2016

Convert all Numeric Variables to Character using proc SQL

The task is to create a successor to a SAS data set, replacing each numeric variable in the original with a character variable. Here is a method which preserves variable order and variable attributes. First create a data set for the demonstration:

procsql;
createtable mixedtype as
 select*
  fromsashelp.class;

quit;

Result:

                  Age as
                    of 1
 First             March
 Name      Sex      2008    Height  Weight
 -----------------------------------------
 Alfred    M          14        69  112.50
 Alice     F          13      56.5   84.00
 Barbara   F          13      65.3   98.00
 Carol     F          14      62.8  102.50
 Henry     M          14      63.5  102.50
 James     M          12      57.3   83.00
 Jane      F          12      59.8   84.50
 Janet     F          15      62.5  112.50
 Jeffrey   M          13      62.5   84.00
 John      M          12        59   99.50
 Joyce     F          11      51.3   50.50
 Judy      F          14      64.3   90.00
 Louise    F          12      56.3   77.00
 Mary      F          15      66.5  112.00
 Philip    M          16        72  150.00
 Robert    M          12      64.8  128.00
 Ronald    M          15        67  133.00
 Thomas    M          11      57.5   85.00
 William   M          15      66.5  112.00

Next, this somewhat tricky query which uses DICTIONARY.COLUMNS as a source of metadata and creates the bulk of another query:

procsql;
selectcase type
        when'num'  thencatx (   ' '
                                , 'left( put ('
                                , name
                                , ','
                                , case
                                   whenformat is null then 'best12.'
                                   elseformat
                                   end
                                , ') ) as'
                                , name
                                , 'label ='
                                , quote( strip(label) )                              
                              )
        when'char' then name
        else             catx (   ' '
                                , quote('Error on type')
                                , 'as'
                                , name
                              )
        end
 into: selections separated by ' , '
 fromdictionary.columns
 wherelibname='WORK' andmemname='MIXEDTYPE';


To adapt for other tables change only the literals in the WHERE clause.

Result:

 Name
 Sex
 left( put ( Age , best12. ) ) as Age label = "Age as of 1 March 2008"
 left( put ( Height , best12. ) ) as Height label = ""
 left( put ( Weight , 6.2 ) ) as Weight label = ""

The INTO clause takes these expressions and concatenates them into a comma-separated string which it stores in the macro variable SELECTIONS, which in turn is used in the statement which actually does the work:

procsql;
createtable allchar asselect &selections frommixedtype;

quit;

Result:

 First          Age as of 1
 Name      Sex  March 2008    Height        Weight
 -------------------------------------------------
 Alfred    M    14            69            112.50
 Alice     F    13            56.5          84.00
 Barbara   F    13            65.3          98.00
 Carol     F    14            62.8          102.50
 Henry     M    14            63.5          102.50
 James     M    12            57.3          83.00
 Jane      F    12            59.8          84.50
 Janet     F    15            62.5          112.50
 Jeffrey   M    13            62.5          84.00
 John      M    12            59            99.50
 Joyce     F    11            51.3          50.50
 Judy      F    14            64.3          90.00
 Louise    F    12            56.3          77.00
 Mary      F    15            66.5          112.00
 Philip    M    16            72            150.00
 Robert    M    12            64.8          128.00
 Ronald    M    15            67            133.00
 Thomas    M    11            57.5          85.00
 William   M    15            66.5          112.00

Another query against DICTIONARY.COLUMNS compares the attributes of the original and replacement variables:

 select   name    format=$10.
        , memname format=$10.
        , varnum  format=10.
        , type    format=$8.
        , length  format=6.
        , format  format=$8.
        , label   format=$30.
  fromdictionary.columns
  wherelibname='WORK' andmemname in ('ALLCHAR','MIXEDTYPE')
  orderby varnum, memname desc;

 quit;

Result:

                             Column
 Column      Member       Number in  Column    Column  Column
 Name        Name             Table  Type      Length  Format    Column Label
 --------------------------------------------------------------------------------------
 Name        MIXEDTYPE            1  char           8            First Name
 Name        ALLCHAR              1  char           8            First Name
 Sex         MIXEDTYPE            2  char           1  $1.
 Sex         ALLCHAR              2  char           1  $1.
 Age         MIXEDTYPE            3  num            8            Age as of 1 March 2008
 Age         ALLCHAR              3  char          12            Age as of 1 March 2008
 Height      MIXEDTYPE            4  num            8
 Height      ALLCHAR              4  char          12
 Weight      MIXEDTYPE            5  num            8  6.2
 Weight      ALLCHAR              5  char           6

This demonstrates that variable order and variable attributes are preserved.

Keyboard Shortcuts within the SAS Main Window




Category Key Combination Action
Dialog Boxes and Entry Fields Tab move to next field
Shift + Tab move to previous field
Navigate around Text
Ctrl + -> (right arrow) move to next word
Ctrl + <- (left arrow) move to previous word
Home move to beginning of line
End move to end of line
Ctrl + Home move to top
Ctrl + End move to bottom
Page Up page up
Page Down page down
Ctrl + Page Up move to top
Ctrl + Page Down move to bottom
Ctrl + Tab navigate to the next open SAS window (NEXTWIND command)
Ctrl + Shift + Tab navigate to the previous open SAS window (PREVWIND command)
Mark Text Shift + -> (right arrow) mark while going to the right
Shift + <- (left arrow) mark while going to the left
Shift + Home mark to beginning of line
Shift + End mark to end of line
Shift + Ctrl + Home mark to top
Shift + Ctrl + End mark to bottom
Shift + Page Up page up and mark
Shift + Page Down page down and mark
Shift + Ctrl + Page Up mark to top
Shift + Ctrl + Page Down mark to bottom
Shift + MB1 extend the current marked text selection to the click position
Cut, Copy, and Paste Delete delete the next character (or marked text)
Ctrl + Delete delete from the insertion point position to the end of the current word
Ctrl + Backspace delete from the insertion point position to the start of the current word
Ctrl + MB1 selects the entire line (clicked line)
Ctrl + Z undo previous action
Ctrl + X cut selected text
Ctrl + C copy selected text to paste buffer
Ctrl + V paste text
Window Control Alt switch focus to or from the main menu bar
Shift + F5 cascade the windows
Shift + F4 tile the windows vertically
Shift + F3 tile the windows horizontally
Ctrl + F6 next window
Alt + F4 exit SAS
Ctrl + F4 close the active window
Ctrl + W access new SAS explorer window
Shift + F10 open pop-up menu
Resizing the Docking View Alt + W + S start docking view resizing
-> (right arrow) move the split bar a small amount to the right
<- (left arrow) move the split bar a small amount to the right
Ctrl + -> (right arrow) move the split bar a larger amount to the right
Ctrl + <- (left arrow) move the split bar a larger amount to the left
Home move the split bar all the way to the left
End move the split bar all the way to the right
Return accept the current size of the docking view and exit docking view resizing
Esc end docking view resizing without resizing the docking view
Miscellaneous
Alt + Enter open the Properties dialog box for a selected object
This command is valid only in a Tree view or a List view.
Esc + letter (or number) color or highlighting attributes in NOTEPAD window


for more   click here




Keyboard Shortcuts within the Enhanced Editor







Category Command Keyboard Shortcut
Abbreviation Add a new abbreviation Ctrl + Shift + A
Bring up word tip Alt + F1 + No Selection
Hide the current word tip Esc
Code Collapsing Collapse all tiered blocks Alt + Ctrl + Number pad -
Collapse current line Alt + Number pad -
Expand all tiered blocks Alt + Ctrl + Number pad +
Expand current line Alt + Number pad +
Toggle expand current line Alt + Number pad *
Command and Macro Support Add or change macros Ctrl + Shift + M
Execute the last recorded macro Ctrl + F1
Play a command and macro Alt + F8
Start and Complete macro Alt + Shift + R
Edit Copy selection Ctrl + C
Ctrl + Insert
Cut selection Ctrl + X
Shift+Delete
Delete current character Delete
Note: You can delete the entire line by using SHIFT+END to extend the selection to the end of the line and pressing the delete or backspace key.
Delete previous character Backspace or Shift + Backspace
Delete to next word start Ctrl + Delete
Delete to previous word start Ctrl + Backspace
Insert a carriage return Enter
Paste from clipboard Ctrl + V
Shift+Insert
Redo Ctrl + Y
Alt + Shift + Backspace
Undo Ctrl + Z
Alt + Backspace
Help Get Help for a SAS procedure place the insertion point within a procedure name and press F1
Context Help F1
Line Markers Go to the next marked line F2
Go to the previous marked line Shift + F2
Toggle marker on the current line Ctrl + F2
Navigation Go to line (interactive) Ctrl + G
Move cursor to the top of the file Ctrl + Page Up
Ctrl + Home
Move cursor to the bottom of the file Ctrl + Page Down
Ctrl + End
Move cursor down Down
Move cursor down a page Page Down
Move cursor left Left
Move cursor right Right
Move cursor to beginning of line Home
Move cursor to end of line End
Move cursor to matching brace and parentheses Ctrl + [
Ctrl + ]
Move cursor to matching DO and END keyword Alt + [
Alt + ]
Move cursor to next case change Alt + Right
Move cursor to next word start Ctrl + Right
Move cursor to previous case change Alt + Left
Move cursor to previous word start Ctrl + Left
Move cursor up Up
Move cursor up a page Page Up
Move cursor to the first visible line Alt + Up
Move cursor to the last visible line Alt + Down
Scroll screen down Ctrl + Up
Scroll screen up Ctrl + Down
Option Setting Toggle insert and overwrite mode Insert
Selection Extend selection character left Shift + Left
Extend selection character right Shift + Right
Extend selection down Shift + Down
Extend selection down a page Shift + Page Down
Extend selection to beginning of document Ctrl + Shift + Home
Ctrl + Shift + Page Up
Extend selection to beginning of line Shift + Home
Extend selection to end of document Ctrl + Shift + End
Ctrl + Shift + Page Down
Extend selection to end of line Shift + End
Extend selection to next case change Alt + Shift + Right
Extend selection to previous case change Alt + Shift + Left
Extend selection up Shift + Up
Extend selection up a page Shift + Page Up
Extend selection to previous word start Ctrl + Shift + Left
Extend selection to the next word start Ctrl + Shift + Right
Select all Ctrl + A
Selection Operations Clean up whitespace characters Ctrl + Shift + W
Comment the selection with line comments Ctrl + /
Convert the selected text to lowercase Ctrl + Shift + L
Convert the selected text to uppercase Ctrl + Shift + U
Tab selection Tab + Selection
Undo the Comment Ctrl + Shift + /
Left Tab selection Shift + Tab + Selection

for more click here