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

I have a data set that I want to add known constants as a new column to the dataset. The constants are not likely to change.

 

data money;
	infile datalines delimiter=",";
	input name $ return invested;
	datalines;
Joe,10,100
Bob,7,50
Mary,80,1000
;

this is the existing dataset, and I now want to add the column `height`. (I know this is silly but its for the question). My current attempt is this:

data returns;
    set returns;
        heights(m) = 1.8, 2.1, 1.6;
run;

The end result I want is something like this.

/* name     | return | invested | height(m)*/
/* ____________________________________________ */
/* Joe      |  10   | 100       |  1.8 */
/* Bob      |   7   | 50        | 2.1 */
/* Mary     | 80    | 50        | 1.6 */

how could I do something like that?

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Personally, this is what I'd do:

 

data heights;
	infile datalines delimiter=",";
	input name $ height;
	datalines;
Joe,1.8
Bob,2.1
Mary,1.6
;

Then merge it by name, not a fan of implicit merges based on row positions. 

 

proc sort data=money; by name;
proc sort data=heights; by name;

data combined;
merge money heights;
by name;
run;

@heyyou1 wrote:
The dataset is coming from a data set about the surrounding population and their health status. The constant data is the total population estimate by race. The total population estimates by race can be found on google and are used for calculating percentages within that data set.

 

View solution in original post

4 REPLIES 4
Reeza
Super User
You would rarely ever process data like that manually. Can you explain your actual situation? Would the data come from a data set, sql, JSON?

heyyou1
Fluorite | Level 6
The dataset is coming from a data set about the surrounding population and their health status. The constant data is the total population estimate by race. The total population estimates by race can be found on google and are used for calculating percentages within that data set.
Reeza
Super User

Personally, this is what I'd do:

 

data heights;
	infile datalines delimiter=",";
	input name $ height;
	datalines;
Joe,1.8
Bob,2.1
Mary,1.6
;

Then merge it by name, not a fan of implicit merges based on row positions. 

 

proc sort data=money; by name;
proc sort data=heights; by name;

data combined;
merge money heights;
by name;
run;

@heyyou1 wrote:
The dataset is coming from a data set about the surrounding population and their health status. The constant data is the total population estimate by race. The total population estimates by race can be found on google and are used for calculating percentages within that data set.

 

Tom
Super User Tom
Super User

@heyyou1 wrote:
The dataset is coming from a data set about the surrounding population and their health status. The constant data is the total population estimate by race. The total population estimates by race can be found on google and are used for calculating percentages within that data set.

Let's create an example that more closely matches this description than the code in your original question.

So you have some data that includes RACE as one of the variables.

data have;
  input race :$40. var1 var2;
cards;
....

And you have your population data by race.

data population;
  input race :$40. total;
cards;
....

You then merge it with your dataset that has the population by RACE.

Now you have the population number on every observation of your original data (at least the ones that have race categories that match).

data want;
  merge have population;
  by race;
  calculation1 = var1/total;
run;

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 1641 views
  • 3 likes
  • 3 in conversation