DATA Step, Macro, Functions and more

how to create a variable and assign a value in sas data step hash object

Accepted Solution Solved
Reply
Occasional Contributor mj5
Occasional Contributor
Posts: 18
Accepted Solution

how to create a variable and assign a value in sas data step hash object

 I have hash object datastep and in that need to create a variable like domain="SE" but it is not happening. any reason where i did something wrong. any guidance?

	data se;
		set startdates;		
		by usubjid
			descending startdate
			descending taetord
			element;
		if _n_=0 then set enddates raw.ta;
		if _N_ = 1 then do;
			declare hash lastdate(dataset:"enddates");
			lastdate.definekey("usubjid");
			lastdate.definedata("dsendtc");
			lastdate.definedone();

			declare hash Tarm(dataset:"raw.TA");
			Tarm.definekey("ETCD");
			Tarm.definedata("EPOCH");
			Tarm.definedone();
		end;
		length oldstart $20;
		retain oldstart;
		SESTDTC=scan(DSSTDTC,1,'T');
		rc1=Tarm.find();
		if not first.usubjid then do;
			SEENDTC=scan(oldstart,1,'T');
		end;
		else do;
			rc=lastdate.find();
			if rc=0 then SEENDTC=scan(DSENDTC,1,'T');
		end;
		if missing(sestdtc) and not missing(seendtc) then delete;
		if SEENDTC>SESTDTC or first.usubjid then output;
		oldstart=DSSTDTC;
        rename TAETORD=SESEQ;
		domain='SE';
		drop rc rc1 dsstdtc startdate enddate domain tatrans tabranch oldstart dsendtc arm armcd TESTRL;
	run;

Accepted Solutions
Solution
‎11-03-2017 06:55 AM
Super User
Super User
Posts: 9,416

Re: how to create a variable and assign a value in sas data step hash object

Whilst its great to use new technology, would it not be simpler, and far easier to read to just do two steps here:

proc sort data=enddates nodupkey;
  by usubjid descending dsendtc;
run;

data se;
  merge raw.ta enddates;
  by usubjid;
  ... /* Do you other processing here */
run;

Its only worth using some technology if there is a benefit to the code for using that technology, and in this case the task can be taken care of with simpler, shorter code (and most likely with less memory overhead).  Avoid using something just for the purpose of using it.

View solution in original post


All Replies
Super User
Posts: 9,890

Re: how to create a variable and assign a value in sas data step hash object

When you have a problem:

- post the complete log of the step (or excerpts if same messages are repeated)

- example data to run the code against. Use a data step with datalines.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Solution
‎11-03-2017 06:55 AM
Super User
Super User
Posts: 9,416

Re: how to create a variable and assign a value in sas data step hash object

Whilst its great to use new technology, would it not be simpler, and far easier to read to just do two steps here:

proc sort data=enddates nodupkey;
  by usubjid descending dsendtc;
run;

data se;
  merge raw.ta enddates;
  by usubjid;
  ... /* Do you other processing here */
run;

Its only worth using some technology if there is a benefit to the code for using that technology, and in this case the task can be taken care of with simpler, shorter code (and most likely with less memory overhead).  Avoid using something just for the purpose of using it.

Super User
Posts: 9,890

Re: how to create a variable and assign a value in sas data step hash object

And if you're just after a quick method to do a lookup without sorting, consider using formats created from raw.ta and enddates.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 113 views
  • 1 like
  • 3 in conversation