As the first step in the decommissioning of sasCommunity.org the site has been converted to read-only mode.


Here are some tips for How to share your SAS knowledge with your professional network.


Solutions in the Round -- Converting from Character to Numeric and Numeric to Character

From sasCommunity
Jump to: navigation, search

Introduction

From the WUSS abstract for this topic

We will identify several ways to convert from character to numeric and numeric to character, and then discuss the efficiency of each method. Solutions in the Round is an exciting new section [at WUSS 2014]. The format is simple: we sit in a circle and discuss the many ways to approach and address a programming problem, and we'll identify situations where one approach might be more useful than another. We will keep notes, and those notes will be posted on sasCommunity.org where we can continue the discussion and allow other to join in after the conference. We hope to engage users from all different perspectives and experience levels to participate in these discussions.

Discussion at WUSS

  • create a new variable?
  • ask client to reformat?
  • who types the names? rename?
  • WHY do we have to convert?
  • 1e8 and other scientific notation can be hard to convert to character
  • keeping leading zeros should be considered (zw. format)

CHARACTER -> NUMERIC Example

DATA NEW;
DATA OLD (rename=(age=agec);
   age=input(agec, best.);
RUN;

Problem: what if you want to join two tables with different data types?

TBL1 INNER JOIN TBL2
   on (TBL1.ZIP = inputc(TBL2.ZIP, best32.)
.......
or 
(PUT(TBL1.ZIP, Z5.) = TBL2.ZIP)
  • IF/THEN/ELSE lookup table

CHARACTER -> NUMERIC Example

DATA C_to_N;
   array(*) _c $ _character_;
   array(*) _n _numeric_;
   do I=1 to dim(_c);
     _n(I)=1*_c(I);
   end;
RUN;

Convert to numeric? CHARACTER -> NUMERIC Example

CHARVAR+0;
  • what about cases where a comma is used in place of a decimal point?
    • compress out non-numerals?

CHARACTER -> NUMERIC Example

NUM=COMPRESS(char, "dk");
input(strip(zip), best32.);

char num 10 1.0 20 2.0 3.5 3.5

best w.d format 5.1 format

Further Discussion -- Open to All

Please join the conversation! Also, if you were one of the live participants, please feel free to correct any mistakes or omissions from our original discussion.

  • First of all, my apologies for the delay in posting this to the site -- several months after the conference. It was a lot of fun to host this section and to collaborate in such a free-formatted section, and I hope that we can continue the discussion here. As the section chair, and in hopes of breaking the ice, I challenged myself to propose the worst solution to every problem. I think that it is important to identify the full spectrum of solutions to a problem, so that we can also identify the things that we are trying to avoid. Also, we can all agree that there is usually not just one solution that fits every single problem. I look forward to hearing others' thoughts. Cheers, --Otterm1 (talk) 10:58, 30 December 2014 (CST)

For numeric to character I was surprised that no one mentioned proc transpose (e.g., https://communities.sas.com/thread/63997 ) --Art T 18:12, 3 January 2015 (CST)