DATA Step, Macro, Functions and more

Creating a variable to count occurences

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

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
Trusted Advisor
Posts: 1,022

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;

View solution in original post


All Replies
Respected Advisor
Posts: 4,936

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.'

Respected Advisor
Posts: 4,936

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!

Trusted Advisor
Posts: 1,022

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
Trusted Advisor
Posts: 1,022

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.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 264 views
  • 0 likes
  • 3 in conversation