## Creating a variable to count occurences

Solved
Occasional Contributor
Posts: 5

# Creating a variable to count occurences

I have a data set that have 48 temperatures that correspond to two 24 hour days. I want to create a variable that is 1 if it occurs in the first day and 2 if it occurs in the second. I used a do loop to create the Hour variable.

data temps

set hot;

do Hours=1 to 24;
input temp@@;
output;
end;
run;

Accepted Solutions
Solution
‎11-29-2016 11:25 PM
Posts: 1,309

## Re: Creating a variable to count occurences

Reading raw data? In that case;

data want;

infile '....... path to raw data of temps ';

do day=1 to 2;

do hour=1 to 24;

input temp @@;

output;

end;

end;

run;

All Replies
Posts: 5,475

## Re: Creating a variable to count occurences

What is the structure of dataset hot?

PG
Occasional Contributor
Posts: 5

## Re: Creating a variable to count occurences

It is a raw text file read in. I subsituted the actual pathway for 'HOT.'

Posts: 5,475

## Re: Creating a variable to count occurences

Something like this?

``````data temps;
infile "path\myHotFile.txt";
do day = 1, 2;
do Hour=1 to 24;
input temp@@;
output;
end;
end;
run;``````
PG
Occasional Contributor
Posts: 5

## Re: Creating a variable to count occurences

Awesome! Thanks for the help!

Posts: 1,309

## Re: Creating a variable to count occurences

Are you reading SAS data from sas dataset HOT?  If so you don't need the INPUT statement which is intended to read raw data.

So lets assume you have 48 obs, i.e. 24 for day 1 and 24 for day 2.

Then you could do this:

data temps;

set hot;

if _n_<=24 then day=1;

else day=2;

run;

The automatic variable _N_ above is the iteration number of the data set, which corresponds in this case to the observation number.

Occasional Contributor
Posts: 5

## Re: Creating a variable to count occurences

I was reading in a raw text file, but to save space and some personal details I subsituted the path for 'HOT.' I initially tried to use the _N_ for this, but I can't seem to get it to work correctly with the do loop. Any ideas?

Solution
‎11-29-2016 11:25 PM
Posts: 1,309

## Re: Creating a variable to count occurences

Reading raw data? In that case;

data want;

infile '....... path to raw data of temps ';

do day=1 to 2;

do hour=1 to 24;

input temp @@;

output;

end;

end;

run;

Occasional Contributor
Posts: 5

## Re: Creating a variable to count occurences

Perfect! Thanks so much!

☑ This topic is solved.