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

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;

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

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;

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

8 REPLIES 8
PGStats
Opal | Level 21

What is the structure of dataset hot?

PG
nickc10
Calcite | Level 5

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

PGStats
Opal | Level 21

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
nickc10
Calcite | Level 5

Awesome! Thanks for the help!

mkeintz
PROC Star

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.

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
nickc10
Calcite | Level 5

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?

mkeintz
PROC Star

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;

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
nickc10
Calcite | Level 5

Perfect! Thanks so much!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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