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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

8 REPLIES 8
Astounding
PROC Star

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;

dapenDaniel
Obsidian | Level 7

But the new variable is missing value.

Reeza
Super User

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.


 

dapenDaniel
Obsidian | Level 7

Hi, the dataset is panel data. 

Reeza
Super User

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. 


 

novinosrin
Tourmaline | Level 20
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;
Astounding
PROC Star

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.

Reeza
Super User

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. 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 8 replies
  • 1201 views
  • 2 likes
  • 4 in conversation