BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Songchan
Calcite | Level 5

Hello guys,

 

In my data, "permno" is character value, i tried to convert it into numeric value, but it didn't work.

data temp02; set temp01;
     if permno=. then delete; 
	 format startdate enddate yymmddn8.;
	 keep permno StartDate enddate ticker;
run;

data temp02; set temp02;
     permno= input(permno, 8.);
run;
1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

It can't be done.  Once a variable is character, it remains character forever.  You can program around that.  But first, to refer to missing values for a character variable, use a blank within quotes:

 

data temp02;
   set temp01;
   if permno = ' ' then delete;
   format startdate enddate yymmddn8.;
   keep permno StartDate enddate ticker;
run;

Your original syntax would be correct for a missing value for a numeric variable.

 

To get a numeric variable from the character PERMNO, you need to create a new variable:

 

data temp03;
   set temp02
   permno_c = input(permno, 8.);
run;

If you want to re-use the same variable name (PERMNO), you can add to the same DATA step:

 

data temp03;
   set temp02;
   permno_c = input(permno, 8.);
   drop permno;
   rename permno_c = permno;
run;

That's what it takes to come up with PERMNO as a numeric variable.

View solution in original post

7 REPLIES 7
r_behata
Barite | Level 11

Could you elaborate on what you meant by didn't work ? post the log.

 

In this statement you are treating permno as if it is numeric

 

if permno=. then delete; 

 but in the next step you are trying to convert permno to numeric.

 

permno= input(permno, 8.);

 

 

SASKiwi
PROC Star

You have to create a new variable when converting.

data temp02; set temp02;
     permno_num= input(permno, 8.);
run;
Songchan
Calcite | Level 5

Thank you!

Songchan
Calcite | Level 5

Hi, the first picture is the contents before converting format, the second is the log of changing formatCapture.PNGCapture1.PNG

Reeza
Super User

@Songchan wrote:

Hi, the first picture is the contents before converting format, the second is the log of changing formatCapture.PNGCapture1.PNG


Check the line of the code indicated in the log - Line 163 in your log will tell you exactly where in your code is the issue. Is that your conversion line? It helps if you show the full log. As others have indicated you cannot change in place, you need to rename the variable ... we could tell you what's wrong if you show the code.

Astounding
PROC Star

It can't be done.  Once a variable is character, it remains character forever.  You can program around that.  But first, to refer to missing values for a character variable, use a blank within quotes:

 

data temp02;
   set temp01;
   if permno = ' ' then delete;
   format startdate enddate yymmddn8.;
   keep permno StartDate enddate ticker;
run;

Your original syntax would be correct for a missing value for a numeric variable.

 

To get a numeric variable from the character PERMNO, you need to create a new variable:

 

data temp03;
   set temp02
   permno_c = input(permno, 8.);
run;

If you want to re-use the same variable name (PERMNO), you can add to the same DATA step:

 

data temp03;
   set temp02;
   permno_c = input(permno, 8.);
   drop permno;
   rename permno_c = permno;
run;

That's what it takes to come up with PERMNO as a numeric variable.

Songchan
Calcite | Level 5

Thank you, that works!

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 1058 views
  • 0 likes
  • 5 in conversation