Solved
Contributor
Posts: 33

# How can i create dummy values for each subject

I want to create dummy values as for every subject i want to have values from 100 to 700. such that i will get for example 10 subjects = 10*700 observations.

Accepted Solutions
Solution
‎04-11-2014 12:17 PM
Super User
Posts: 8,097

## Re: How can i create dummy values for each subject

To make the template just use a DO loop on a list of subjects.

data template;

set have(keep=subject);

by subject;

if first.subject;

do nom_time = -300 to 730 ;

output;

end;

run;

Then you can merge it with your existing data.

data want ;

merge template have ;

by subject nom_time ;

run;

All Replies
Posts: 2,655

## Re: How can i create dummy values for each subject

Search the site for "simulation'.  There are a LOT of posts where this is undertaken.  You could easlily adapt any of them to your situation.

Steve Denham

Contributor
Posts: 33

## Re: How can i create dummy values for each subject

simulation gives random values. i want in serial order starting from 100 to 700 for each subject.

Posts: 2,655

## Re: How can i create dummy values for each subject

Wouldn't two nested do loops (outer for subject, inner going from 100 to 700) give you what you need?  I am curious as to what you need 600 levels for, though.

Steve Denham

Regular Contributor
Posts: 217

## Re: How can i create dummy values for each subject

data want;

set have;    /* table with 10 rows */

do i=1 to 700;
serial_value = i;

output;

end;

run;

Contributor
Posts: 33

## Re: How can i create dummy values for each subject

I am sorry for the confusion . i typed the full message but it was not showing.

I need a dataset which has subj,  value and nom time(range between -320 to 720).

i want to create a dummy  variable in which for a subj if the maximum nom time is 120 then from 120 to 720 by 1  duplicate rows with missing value and the rest carry forward.

Posts: 2,655

## Re: How can i create dummy values for each subject

A short example of the data that reflects all of this would be extremely helpful.

Steve Denham

Contributor
Posts: 33

## Re: How can i create dummy values for each subject

this is my data

 subj value nom_time 1 12 -320 1 1 12 1 234 13 1 22 120 1 44 121 1 3 122

i want the data like the below

 subj value nom_time 1 12 -320 1 1 12 1 234 13 1 22 120 1 44 121 1 3 122 1 . 123 1 . 124 1 . 125 1 . 126 1 . 127 1 . 128 1 . 129 1 . 130 1 . 130 and soo on till 720 2 2 12 2 6 13 2 4 120 2 3 121 2 5 122 2 6 123 2 . 124 2 125 2 . 126 2 . 127 2 128 2 . 129 2 130 2 . 130 and soo on till 720
Super User
Posts: 9,599

## Re: How can i create dummy values for each subject

Something like:

data have;
attrib subj value nom_time format=best.;
infile datalines;
input subj value nom_time;
datalines;
1 12 -320
1 1 12
1 234 13
1 22 120
1 44 121
1 3 122
;
run;
data want;
set have;
by subj;
if last.subj then do;
output;
do i=1 to 720-nom_time;
nom_time=i;
value=.;
output;
end;
end;
run;

Contributor
Posts: 29

## Re: How can i create dummy values for each subject

first sort your original dataset then create loop for the required data.

data new;

do subj = 1 to 10; output;

end

run;

data new1;

set new;

do nom_time =1 to 720; output;

end;

run;

data final;

merge originaldataset new2;

by subj nom_time;

run;

Super User
Posts: 6,773

## Re: How can i create dummy values for each subject

This sounds like what you are shooting for:

data want;

set have;

by subject;

output;

if last.subject and nom_time < 720;

value=.;

do nom_time = nom_time + 1 to 720;
output;

end;

run;

Contributor
Posts: 33

## Re: How can i create dummy values for each subject

somehow it is not working. i will explain in detail again.

I need to keep nom_tim till 720 for every subj. but in the data i have 200 as last nom_tim for some subj and 340 etc for others. In this case i need  to impute nom_tim with missing value so that i can carry forward last available value till nom_time 720 for all subjs.

Super User
Posts: 9,599

## Re: How can i create dummy values for each subject

Hi,

Either mine or Astounding's solution gives the required output per your example above.  Could you clarify 'what does not work'.

Contributor
Posts: 33

## Re: How can i create dummy values for each subject

i want one dataset with subject corresponding to nom_time 100 to 700 as a template. another dataset with original. then i want to join them so that from the template i can replace the nom_time. for example a subj 1200 has nom_tim till 200 then i can replace with the template and make them till 700

Super User
Posts: 6,773

## Re: How can i create dummy values for each subject

My solution does all of that in a single short step ... no need for a template, no need to join.  At least based on the description you gave of the desired output, it produces what you say you want as your final result.

🔒 This topic is solved and locked.