DATA Step, Macro, Functions and more

Command for populating a character variable as numeric in output files

Reply
Occasional Contributor
Posts: 5

Command for populating a character variable as numeric in output files

[ Edited ]

Hi Guys, 

I need a command used to populate a variable as numeric in datasets. The visit number variable for a visit 99.1.01 is considered as character variable in SAS program since it has 2 decimal values. I need to populate this value (99.1.01) as a numeric value. Kindly suggest me how to move forward with it. There are totally 8 other variables which are working fine. Only the VISIT variable has issue. I have pasted the program below.

 

data datadir.ABCD;


label STUDY = 'Study Code'
SUBJECT = 'Enrolment Code'
VISIT = 'Visit Number'
SPECDAT = 'Date of Collection'
SPECTIM = 'Time of Collection'
SPECID = 'Specimen ID'
PROTSCHD = 'Protocol Schedule'
COMMENTS = 'Specimen Comments';

 

length STUDY $11.
SUBJECT $8.
VISIT 8.
SPECDAT $10.
SPECTIM $5.
SPECID $20.
PROTSCHD $20.
COMMENTS $200.;

 

infile datafile delimiter = '|' dsd missover lrecl=2000;

input STUDY SUBJECT VISIT SPECDAT SPECTIM SPECID PROTSCHD COMMENTS;

 

run:

 

Many Thanks,

Harsha

Super User
Posts: 7,757

Re: Command for populating a character variable as numeric in output files

99.1.01 can never be a numeric value. Numeric values can only have one decimal point, period. That's basic calculus.

So you need to make a decision

Is it

991.01

99.101

99101

990101

99001001

.....

?

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 5

Re: Command for populating a character variable as numeric in output files

Posted in reply to KurtBremser
Dear KurtBremser,
Thanks for your quick feedback. I was checking if we can populate the
character value (99.1.01) as a numeric value in data file.
That is, any command which could be use to get the desired output.
Regards,
Harsha

##- Please type your reply above this line. Simple formatting, no
attachments. -##
Super User
Posts: 7,757

Re: Command for populating a character variable as numeric in output files

You must supply the "desired output", then we can help you with code that creates that output.

Let me repeat:

99.1.01 is NOT a number, therefore you can't store it in a numeric variable.

 

You can, for instance, split it into several numeric variables:

data want;
set have;
num1 = input(scan(charval,1,'.'),2.);
num2 = input(scan(charval,2,'.'),2.);
num3 = input(scan(charval,3,'.'),2.);
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 5

Re: Command for populating a character variable as numeric in output files

Posted in reply to KurtBremser

Dear All,

 

Many thanks for all your support. I checked with few of my collegues and SAS tech team and found out that we cannot provide a SAS output as a numeric for values with two periods (99.1.01). I have initiated a programming request in our database by changing logic of this kind of outputs. 99.101.

 

Thanks,

Harsha

Trusted Advisor
Posts: 1,117

Re: Command for populating a character variable as numeric in output files

Hi @dhaha001,

 

You could also consider storing the three components of your visit numbers (e.g. 99, 1 and 01) in three separate (possibly numeric) variables. This would facilitate grouping and selection of observations based on these variables.

 

You should definitely avoid numeric visit numbers like 991.01, i.e. (mis)using the decimal point as a mere separator between two ordinal values. (Reason: risk of numeric representation issues.)

 

Another option would be to create a format that assigns labels like "99.1.01" to numeric visit numbers (still to be defined) such as 1, 2, 3, ...

Super User
Super User
Posts: 7,942

Re: Command for populating a character variable as numeric in output files

I agree with both @KurtBremser and @FreelanceReinhard on both their comments other than format, as whilst you could force it using a format, I don't believe that is good working practice and would just be confusing.  Also be aware that that "visit" number is not valid per standard data models - SDMT/ADaM.  There are plenty of different ways of setting up visit numbering, for instance visit + repeat number + timepoint.  I would suggest from your example that 99 indicates an unscheduled visit, 1 being the first repeat, and 01 being the timepoint thus:

AVISITN            ATPTN

99.1                  1   

If you were using ADaM.

Super User
Posts: 11,335

Re: Command for populating a character variable as numeric in output files

As an aside it looks like you have a date and a time variable that are character. You may want to consider using an appropriate date or time informat and assigning date and time formats to those variables. If you ever want to do to things like calculating subject age or intervals between visits for the same subject then SAS date and time variables will make it a lot easier with the various functins designed to work with those.

 

And by any chance is that VISIT variable actually a date also? When I saw your example my first thought was 1999, January 1. If so let us know which of the bits is month and day of month.

Ask a Question
Discussion stats
  • 7 replies
  • 333 views
  • 4 likes
  • 5 in conversation