BookmarkSubscribeRSS Feed
Sarra-Matri
Obsidian | Level 7

From an imported tables, how can i create a new table that contains some columns?

13 REPLIES 13
ballardw
Super User

Or if you want to keep most of the variables and just remove a few, use Drop

 

data want;
    set have;
    drop P D Q;
run;
Sarra-Matri
Obsidian | Level 7

thank you for your reply but columns will be imported from multiple tables not just one

 

Sarra-Matri
Obsidian | Level 7

i have a set of tables in my sas studio, from these tables i want to create a table which contains specific columns. and these columns do not belong to the same table

Sarra-Matri
Obsidian | Level 7

From the tables, i have to create a new table that contains the columns following: "account", "code", "country_label", "product_type", value.

Kurt_Bremser
Super User

So what you do is a so-called "lookup" (expanding a code to a derived value).

There are several methods for doing this:

A SQL join:

proc sql;
create table want as
  select
    a.account,
    a.code,
    b.country_label,
    a.product_type,
    a.value
  from gsquarterly_december_2020 a
  left join
  country_classification b
  on a.country_code = b.country_code
;
quit;

Create a format:

data cntlin;
set country_classification;
fmtname = "country_label";
type = "C";
rename
  country_code=start
  country_label=label
;
keep fmtname type start label;
run;

data want;
set have;
country_label = put(country_code,country_label.);
keep account code country_label product_type value;
run;

A data step merge:

proc sort data=country_classification;
by country_code;
run;

proc sort data=gsquarterly_december_2020;
by country_code;
run;

data want;
merge
  gsquarterly_december_2020 (in=a)
  country_classification
;
by country_code;
if a;
keep account code country_label product_type value;
run;

or, most modern, a hash object:

data want;
set gsquarterly_december_2020;
if _n_ = 1
then do;
  length country_label $30; /* use appropriate length here */
  declar hash cc (dataset:"country_classification");
  cc.definekey("country_code");
  cc.definedata("country_label");
  cc.definedone();
end;
if cc.find() ne 0 then country_label = "";
keep account code country_label product_type value;
run;
Sarra-Matri
Obsidian | Level 7
Thank you so much for your help
I have an other question if you can help please
I want to classify account column by product then by country and do a graph.
How i can do that?
Kurt_Bremser
Super User

Please supply an example for your dataset in a data step with datalines, so I have something to work with.

Describe how your graph should look like (line,bar,...) and what should be the X and Y axis values.

Sarra-Matri
Obsidian | Level 7
I have these tables and i want to classify column account (imports
/exports) by product then by country
then i should do a graph to the results