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

Hi all,

 

I have following data, where the state name is embedded in the variable name. I need to create a new variable called state from this.

 

testdata:

periodvar1.IDvar1.ORvar1.NVvar1.WAvar1.CAvar2.IDvar2.ORvar2.NVvar2.WAvar2.CA
1995Q110203040501020304050
1995Q210203040501020304050
1995Q310203040501020304050
1995Q410203040501020304050
1996Q110203040501020304050
1996Q210203040501020304050
1996Q310203040501020304050
1996Q410203040501020304050

 

expected data:

periodstatevar1var2
1995Q1ID1010
1995Q1OR2020
1995Q1NV3030
1995Q1WA4040
1995Q1CA5050
1995Q2ID1010
1995Q2OR2020
1995Q2NV3030
1995Q2WA4040
1995Q2CA5050
1995Q3ID1010
1995Q3OR2020
1995Q3NV3030
1995Q3WA4040
1995Q3CA5050
1995Q4ID1010
1995Q4OR2020
1995Q4NV3030
1995Q4WA4040
1995Q4CA5050

 

Please help me with this.

 

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

This is done with proc transpose:

data have;
infile cards dlm='09'x;
input period $	var1_ID	var1_OR	var1_NV	var1_WA	var1_CA	var2_ID	var2_OR	var2_NV	var2_WA	var2_CA;
cards;
1995Q1	10	20	30	40	50	10	20	30	40	50
1995Q2	10	20	30	40	50	10	20	30	40	50
1995Q3	10	20	30	40	50	10	20	30	40	50
1995Q4	10	20	30	40	50	10	20	30	40	50
1996Q1	10	20	30	40	50	10	20	30	40	50
1996Q2	10	20	30	40	50	10	20	30	40	50
1996Q3	10	20	30	40	50	10	20	30	40	50
1996Q4	10	20	30	40	50	10	20	30	40	50
;
run;

proc transpose data=have out=trans1;
by period;
var var:;
run;

data trans2;
set trans1;
length state $2;
state = scan(_name_,2,'_');
_name_ = scan(_name_,1,'_');
run;

proc sort data=trans2;
by period state;
run;

proc transpose data=trans2 out=want (drop=_name_);
by period state;
var col1;
id _name_;
run;

Note that the only assumption is the prefix and the underline as separator in the original variable name.

Note 2: dots are not valid in SAS variable names, so I changed to underlines.

 

View solution in original post

7 REPLIES 7
PeterClemmensen
Tourmaline | Level 20

How is this data stored? Do you have periods in your variable names?

UshaLatha
Obsidian | Level 7

yes,

we have a variable named "period" which contains all the quarters in character format

Kurt_Bremser
Super User

This is done with proc transpose:

data have;
infile cards dlm='09'x;
input period $	var1_ID	var1_OR	var1_NV	var1_WA	var1_CA	var2_ID	var2_OR	var2_NV	var2_WA	var2_CA;
cards;
1995Q1	10	20	30	40	50	10	20	30	40	50
1995Q2	10	20	30	40	50	10	20	30	40	50
1995Q3	10	20	30	40	50	10	20	30	40	50
1995Q4	10	20	30	40	50	10	20	30	40	50
1996Q1	10	20	30	40	50	10	20	30	40	50
1996Q2	10	20	30	40	50	10	20	30	40	50
1996Q3	10	20	30	40	50	10	20	30	40	50
1996Q4	10	20	30	40	50	10	20	30	40	50
;
run;

proc transpose data=have out=trans1;
by period;
var var:;
run;

data trans2;
set trans1;
length state $2;
state = scan(_name_,2,'_');
_name_ = scan(_name_,1,'_');
run;

proc sort data=trans2;
by period state;
run;

proc transpose data=trans2 out=want (drop=_name_);
by period state;
var col1;
id _name_;
run;

Note that the only assumption is the prefix and the underline as separator in the original variable name.

Note 2: dots are not valid in SAS variable names, so I changed to underlines.

 

UshaLatha
Obsidian | Level 7

hi,..

one question. what is var: in this code mean?? do I need to specify all variable names?

 

proc transpose data=have out=trans1;
by period;
var var:;
run;

UshaLatha
Obsidian | Level 7

Thanks !!!! The code works perfectly !!!

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 812 views
  • 3 likes
  • 3 in conversation