BookmarkSubscribeRSS Feed
jimmychoi
Obsidian | Level 7

Hi,

 

my data is:

Country Code1930193120182019
AND12 34
ATG56 78
ABW910 1112
AUS1314 1516

 

Desired output:

Country CodeValue
AND-19301
AND-19312
AND-…
AND-20183
AND-20194
ATG-19305
ATG-19316
ATG-…
ATG-20187
ATG-20198

 

 

Since this concatenates the names of rows and columns and reduces the dimension of the table,

I honestly don't know how to achieve it with functions such as transpose.

and for the original data, there are about 100 rows (100 different country codes) and 90 columns (from year 1930 - 2019)

Please help.

9 REPLIES 9
Shmuel
Garnet | Level 18

It is not clear - is the 1st row part of your data? or is it the labels row?

It seems that your input is an excel table. Have you imported it to sas?

Are years successive from 1930 up to 2019 ?

 

Assuming your data is already a sas data set, please post your data in form of:

data test;
   input <variables>
datalines;

... your data ...

run;
jimmychoi
Obsidian | Level 7
Hi Shmuel,
yes the first row is for the labels. and the years are successive from 1930 up to 2019.
For that reason, I couldn't write the data in datalines statement.
Shmuel
Garnet | Level 18

Adapt next code to your data:

data test;
   input country $3. value1-value5;
datalines;
AND 1 2 3 4 5
ATG 6 7 8 9 10
;
run;

data want;
     length coutry_code $8 ;
  set test;
    array year y1-y5;
	array vx valu1-value5;
	if _N_ = 1 then 
       do i=1 to 5;
	      yx(i) = i +1929;
	   end;
	do i=1 to 5;
	   country_code = cat('-',country,yx(i));
	   value = vx(i);
	end;
	keep country_code value;
run;
jimmychoi
Obsidian | Level 7
Trying to read and understand the link...
VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

here is a sample that does basically the samething.

data Abun_dw20d_summary ;       informat bins $30.; format bins $30.; length bins $30.;
   input bins $ CM1DW20D CM3DW20D CW1DW20D ODWD W1DW4D;
   datalines;         
Acinetobacter 2477.123456789 195  163 45 77
Actinoalloteichus 2431 220  198 69.123456789 6774
Bacillus 2456 173  155 78 785
Bletilla 2412 135  116.123456789 45 123
;   
run;


proc transpose data=Abun_dw20d_summary out=bint;
    var _all_;
    by bins;
run;


VDD
Ammonite | Level 13 VDD
Ammonite | Level 13
data have;
input Country_Code $ y1930 y1931 y2018 y2019;
cards;
AND
1
2
 
3
4
ATG
5
6
 
7
8
ABW
9
10
 
11
12
AUS
13
14
 
15
16
;
proc sort data=have;
	by country_code;
run;
proc transpose data=have out=want;
    var _all_;
    by country_code;
run;
Kurt_Bremser
Super User

If you really have these column names, proc transpose is the way to go:

proc transpose data=have out=trans;
by country_code;
var _numeric_;
run;

And then create the final dataset in an additional data step:

data want;
set trans (
  rename=(col1=value country_code=country);
);
length country_code $8;
country_code = catx('-',country,_name_);
drop country _name_;
run;
Astounding
PROC Star
Unless you have some special need that you haven't revealed, you would be much better off keeping the YEAR separate from the COUNTRY. Several posters have shown how to do that using PROC TRANSPOSE.

It's to your advantage to learn how to program with data in that form. Here's just one example where three variables make the task easy but two variables make the task difficult: find the average VALUE for each year.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 9 replies
  • 2208 views
  • 1 like
  • 5 in conversation