Copy value to a Character Variable or Numerical Variable depending on value's format.

Reply
Contributor
Posts: 26

Copy value to a Character Variable or Numerical Variable depending on value's format.

I'm looking to create two new variables, one for character values and the other for numeric values, that are meant to have values from another variable that includes both charater and numeric values copied into.

The code below describes what I am trying to do.

 

DATA SAS.DA;

KEEP ID RES RESCHAR RESNUM

RESCHAR=put(RES,char.);

RESNUM=put(RES,num.);

SET SAS.DA;

RUN;

 

The table below shows what I am hoping to generate

 

ID      RES       RESCHAR     RESNUM

01      5                                   5

02      cat          cat

03      dog         dog

04      14                                14

05       7                                  7

 

Any suggestion is greatly appreciated

Grand Advisor
Posts: 10,196

Re: Copy value to a Character Variable or Numerical Variable depending on value's format.

If you can put up with messages about "invalid numeric data" and conversion of character to numeric this code doesn't require any parsing.

data want;
   set have;
   length ResNum 8 ResChar $ 5;
   resnum=res;
   if resnum=. then reschar=res;
run;

Make sure the length assigned to reschar is at least as long as your RES variable.

 

Grand Advisor
Posts: 17,313

Re: Copy value to a Character Variable or Numerical Variable depending on value's format.

If you can assume that numeric will have only numeric values and that character values will have only character you can use ANYALPHA to check for the presence of alphabetic characters. There are also similar functions to check for numbers in a cell. You can also consider supressing the error messages by using ? in the put statement.

 


data have;
input ID $ RES $;
cards;
01 5
02 cat
03 dog
04 14
05 7
;
run;

proc print;run;

data want;
set have;
if anyalpha(res)>0 then resChar=input(res,$3.);
else resNum=input(res, 3.);
run;
Contributor
Posts: 26

Re: Copy value to a Character Variable or Numerical Variable depending on value's format.

This works,

 

DATA SAS.DA;

KEEP ID RES RESCHAR RESNUM

SET SAS.DA;

IF ANYALPHA(RES)>0 THEN RESCHAR=put(RES,$8.);

IF ANYALPHA(RES)=0 THEN RESNUM=put(RES,8.);

RUN;

 

I have tried with INPUT statements instead of PUT and it returns blank columns. I have gathered some insight from the following link but I still can't decide when to use one or the other: http://blogs.sas.com/content/sgf/2015/05/01/converting-variable-types-do-i-use-put-or-input/

 

Another interresting fact is that there are no FORMAT or INFORMAT associated with the variables RESCHAR and RESNUM when I check the PROC CONTENTS. I was under the impresion that the format specified in the PUT statement would transfer to RESCHAR and RESNUM.

 

Anyway, the dataset looks good. Thanks!

 

Grand Advisor
Posts: 9,568

Re: Copy value to a Character Variable or Numerical Variable depending on value's format.

data have;
input ID  $    RES  $;
if notdigit(strip(RES)) then CHAR=RES;
 else NUM=input(RES,best32.);
cards;  
01      5     
02      cat      
03      dog       
04      14                  
05       7
;
run;
Respected Advisor
Posts: 4,606

Re: Copy value to a Character Variable or Numerical Variable depending on value's format.

I would do:

 

data test;
input ID RES $;
resnum = input(RES, ?? best.);
if missing(resnum) then reschar = RES;
datalines;
01      5   
02      cat 
03      dog 
04      14  
05       7  
;
PG
Esteemed Advisor
Esteemed Advisor
Posts: 7,195

Re: Copy value to a Character Variable or Numerical Variable depending on value's format.

Not sure why you would need to create reschar, res is a character variable holding character data - this includes numbers and specials.  So why have a column which doesn't really add anything.  If you were using CDISC standards - not sure if that is your field, then reschar should hold everything, and the nunmeric portion is a subset of reschar where it is possible to convert to numeric.  

 

Contributor
Posts: 26

Re: Copy value to a Character Variable or Numerical Variable depending on value's format.

You are correct, I am in the process of converting an XML ODM file using the CDISC SDTM standards. I posted my problem in this form for the sake of simplicity. I am aware (I think) of the functions of the --ORRES, --STRESC and --STRESN variables but if you have codes you can share to consistently generate --STRESC and --STRESN values from --ORRES across all domains, that would be greatly appreciated.

 

If you are interrested, below is the end of the "real" code I used to wrap up my DA domain which looks OK (the dataset) as far as I can tell.

 

Thanks for the input.

 

*Add --SEQ variable;

PROC SORT DATA = SAS.DA;

by USUBJID DADTC;

RUN;

 

DATA SAS.DA;

SET SAS.DA;

DASEQ + 1;

by USUBJID DADTC;

if first.USUBJID then DASEQ = 1;

RUN;

 

*Select Final Variables;

 

DATA SAS.DA;

KEEP STUDYID DOMAIN USUBJID DASEQ DAREFID DATESTCD DATEST DACAT DASCAT DAORRES DAORRESU DASTRESC DASTRESN DASTRESU VISITNUM DADTC;

SET SAS.DA;

STUDYID="STUDY-X";

DOMAIN="DA";

DAORRESU="Capsules";

VISITNUM=NULL;

DASTRESU=put(DAORRESU,$8.);

DASTRESC=put(DAORRES,$8.);

IF ANYALPHA(DAORRES)=0 THEN DASTRESN=put(DAORRES,8.);

RUN;

 

*Order Final Variables;

PROC SQL;

CREATE TABLE SAS.DA AS

SELECT STUDYID, DOMAIN, USUBJID, DASEQ, DAREFID, DATESTCD, DATEST, DACAT, DASCAT, DAORRES, DAORRESU, DASTRESC, DASTRESN, DASTRESU, VISITNUM, DADTC

FROM SAS.DA;

RUN;

Ask a Question
Discussion stats
  • 7 replies
  • 474 views
  • 1 like
  • 6 in conversation