DATA Step, Macro, Functions and more

How to format character variable from excel data

Reply
Contributor
Posts: 23

How to format character variable from excel data

Below is the code I have but I am getting no observations once I do this step. Please help!

 

data workingdata;
set work.test;
Format CIK $10;

IF COMPANY_FKEY<=9999 THEN CIK =cats('000000',COMPANY_FKEY);

 

I need to create all these values as 10 digits with leading 0's. 

Do I need to define company_fkey before?

 

Thank you!

Contributor
Posts: 46

Re: How to format character variable from excel data

[ Edited ]

Make sure there is a period ('.') after your format... so it should look like FORMAT CIK $10.;

 

And yes, the variable COMPANY_FKEY needs to be defined in the work.test dataset  if you are going to test its value in the workingdata data step.

Contributor
Posts: 23

Re: How to format character variable from excel data

Posted in reply to JasonDiVirgilio

data workingdata;
267 Set work.test;
268 COMPANY_FKEY=CIK;
269 run;

 

271 data workingdata1;
272 Set work.workingdata;
273 Format CIK $10.;
WARNING: Variable CIK has already been defined as numeric.

 

This now shows up. How do I orignally format CIK as numeric---need it to be character since I need the leading zeros. Numeric gets rid of the leading zeros. 

Super User
Posts: 10,571

Re: How to format character variable from excel data

You can't change the type of the variable, you can only replace it by creating a new variable and using rename= and drop= dataset options.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 13,941

Re: How to format character variable from excel data


dlazer1 wrote:

data workingdata;
267 Set work.test;
268 COMPANY_FKEY=CIK; Since CIK already exists here it seems that it was created or made numeric when work.test was created.
269 run;

 

271 data workingdata1;
272 Set work.workingdata;
273 Format CIK $10.;
WARNING: Variable CIK has already been defined as numeric.

 

This now shows up. How do I orignally format CIK as numeric---need it to be character since I need the leading zeros. Numeric gets rid of the leading zeros. 


If you have a numeric variable and need to create a character version then use

Charversion = put(numericversion, Z10.);

the Z format pads the displayed value with leading zeroes to the number of spaces indicated in the format. If the variable has a value of 123 and you use the Z10 it will display, or with put statement as shown create, 0000000123

Contributor
Posts: 23

Re: How to format character variable from excel data

Thank you!

Ask a Question
Discussion stats
  • 5 replies
  • 152 views
  • 0 likes
  • 4 in conversation