Help using Base SAS procedures

Convert string variable to numeric in panel data

Accepted Solution Solved
Reply
Contributor
Posts: 28
Accepted Solution

Convert string variable to numeric in panel data

[ Edited ]

I imported an excel dataset to sas. There is a variable called "Year". The format is as following. When I want to merge this dataset with the other one, there is a warning that the length of "Year" in these two datasets are different. I tried to change the length for this imported dataset, but it failed. How can I convert it to numeric variable? Thanks.

 

My code is 

 

data a3;
length year $ 4;
set a2;
run;

 

 

 The dataset is a panel data.

 

Year

1990

1991

1992

1993

1990

1991

1992

1993


Accepted Solutions
Solution
‎04-16-2018 07:33 PM
Super User
Posts: 6,933

Re: Convert string variable to numeric in panel data

Posted in reply to dapenDaniel

Sorry, I was misled by your title.  From your description of the problem, you don't need to convert anything to numeric.

 

Some steps:

 

 

(1) Run a PROC CONTENTS on each data set that you want to merge, and find the longest length for the variable YEAR in any of the data sets.  Confirm that YEAR is character in all data sets.

 

(2) Before trying to merge data sets, make sure your YEAR variables are left-hand justified.  That's easy to force:

 

year = left(year);

 

(3) In the DATA step that combines the data sets, add a LENGTH statement RIGHT AFTER the DATA statement.

 

length year $ 10;

 

Use the longest length that you discovered in step 1.

View solution in original post


All Replies
Super User
Posts: 6,933

Re: Convert string variable to numeric

Posted in reply to dapenDaniel

You can get there, but you can't change the existing variable from character to numeric while keeping the same name.  To work around that:

 

data want;

set have;

year_num = input(year, 4.);

drop year;

rename year_num = year;

run;

Contributor
Posts: 28

Re: Convert string variable to numeric

Posted in reply to Astounding

But the new variable is missing value.

Super User
Posts: 24,004

Re: Convert string variable to numeric

Posted in reply to dapenDaniel

Post your code and log.

 

Here's @Astounding solution, fully tested, that you can run on your system.

data have;
input year $;
cards;
2011
2012
2013
2014
2015
2016
;;;;
run;

data want;

set have;

year_num = input(year, 4.);

drop year;

rename year_num = year;

run;

proc print data=want;run;
proc contents data=want;run;

@dapenDaniel wrote:

But the new variable is missing value.


 

Contributor
Posts: 28

Re: Convert string variable to numeric

Hi, the dataset is panel data. 

Super User
Posts: 24,004

Re: Convert string variable to numeric

Posted in reply to dapenDaniel

Not sure how that affects this at all. Can you elaborate? Saying it doesn't work is not informative, it conveys no information about what didn't work, if you had an error, or what you even tried.

 


@dapenDaniel wrote:

Hi, the dataset is panel data. 


 

Super User
Posts: 2,061

Re: Convert string variable to numeric in panel data

Posted in reply to dapenDaniel
data have;
input year $;
cards;
2011
2012
2013
2014
2015
2016
;;;;
run;

data want;
length year 8.;
set have(rename=(year=_year));
year=_year;
run;
Solution
‎04-16-2018 07:33 PM
Super User
Posts: 6,933

Re: Convert string variable to numeric in panel data

Posted in reply to dapenDaniel

Sorry, I was misled by your title.  From your description of the problem, you don't need to convert anything to numeric.

 

Some steps:

 

 

(1) Run a PROC CONTENTS on each data set that you want to merge, and find the longest length for the variable YEAR in any of the data sets.  Confirm that YEAR is character in all data sets.

 

(2) Before trying to merge data sets, make sure your YEAR variables are left-hand justified.  That's easy to force:

 

year = left(year);

 

(3) In the DATA step that combines the data sets, add a LENGTH statement RIGHT AFTER the DATA statement.

 

length year $ 10;

 

Use the longest length that you discovered in step 1.

Highlighted
Super User
Posts: 24,004

Re: Convert string variable to numeric in panel data

Posted in reply to dapenDaniel

Length differences do not require conversion, just modify the lengths, in a data step for each variable to be the same and then combine them. Or convert them to numeric as shown where this is a non-issue

 

data new1;
length year $4.;
set old1;
run;

data new2;
length year $4.;
set old2;
run;

PS. Please do not modify your question to change it after the fact, it makes the question misleading. Post new information as a new post. 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 312 views
  • 2 likes
  • 4 in conversation